一. _thread - 多線程
_thread 模塊提供創(chuàng)建新線程的方法,并提供互斥鎖, 線程最多16個(gè)
import _thread
方法 | 描述 |
---|---|
_thread.get_ident() | 獲取當(dāng)前線程號(hào) |
_thread.get_heap_size() | 獲取系統(tǒng)剩余內(nèi)存大小 |
_thread.stack_size(size) | 設(shè)置創(chuàng)建新線程使用的棧大械钟(以字節(jié)為單位)欺劳,默認(rèn)為8k。 |
_thread.start_new_thread(function, args) | 創(chuàng)建一個(gè)新線程铅鲤,接收?qǐng)?zhí)行函數(shù)和被執(zhí)行函數(shù)參數(shù)划提,注意:傳參要傳元組, 當(dāng) function 函數(shù)無(wú)參時(shí)傳入空的元組。 |
_thread.allocate_lock() | 創(chuàng)建一個(gè)互斥鎖對(duì)象邢享。 |
lock.acquire() | 獲取鎖鹏往,成功返回True,否則返回False骇塘。 |
lock.release() | 釋放鎖 |
lock.locked() | 返回鎖的狀態(tài)伊履,True表示被某個(gè)線程獲取,F(xiàn)alse則表示沒有. |
1. 舉例一: 簡(jiǎn)單的多線程并行
import _thread
import utime
counter = 0
def led_func():
while True:
print("這是一個(gè)LED閃燈程序, 我在每2秒瘋狂閃燈!進(jìn)程號(hào)",_thread.get_ident())
utime.sleep(2)
def add1_persecond():
while True:
global counter
counter += 1
print("計(jì)數(shù)加一:",counter,",進(jìn)程號(hào):",_thread.get_ident())
utime.sleep(1)
if __name__ == "__main__":
_thread.start_new_thread(led_func,())
_thread.start_new_thread(add1_persecond,())
2. 帶參數(shù)的進(jìn)程創(chuàng)建
帶參數(shù)創(chuàng)建進(jìn)程要注意: 參數(shù)必須傳元組, 所以如果你只有一個(gè)參數(shù)a
, 也應(yīng)該包裹在元組中 (a,)
, 如果不需要參數(shù)則傳遞空元組()
import _thread
import utime
counter = 100
def add_thread(step):
while True:
global counter
counter += step
print("計(jì)數(shù):",counter,",進(jìn)程號(hào):",_thread.get_ident())
utime.sleep(1)
if __name__ == "__main__":
_thread.start_new_thread(add_thread,(3,))
3. 舉例三: 互斥鎖
add10_persecond的for循環(huán)操作counter時(shí), 避免minus5_persecond的for循環(huán)操作它, 所以加個(gè)互斥鎖, 每個(gè)進(jìn)程, 拿到這個(gè)鎖才能操作公共變量counter
import _thread
import utime
lock = _thread.allocate_lock() #創(chuàng)建一個(gè)互斥鎖
counter = 0
def led_func():
while True:
print("這是一個(gè)LED閃燈程序, 我在每2秒瘋狂閃燈!進(jìn)程號(hào)",_thread.get_ident())
utime.sleep(2)
def add10_persecond():
while True:
lock.acquire()
global counter
for i in range(10):
counter += 1
print("計(jì)數(shù)加一:",counter,",進(jìn)程號(hào):",_thread.get_ident())
lock.release()
utime.sleep(1)
def minus5_persecond():
while True:
lock.acquire()
global counter
for i in range(5):
counter -= 1
print("計(jì)數(shù)減一:",counter,",進(jìn)程號(hào):",_thread.get_ident())
lock.release()
utime.sleep(5)
if __name__ == "__main__":
_thread.start_new_thread(led_func,())
_thread.start_new_thread(add10_persecond,())
_thread.start_new_thread(minus5_persecond,())
4. 線程退出
暫時(shí)沒有提供類似于 join 函數(shù)之類的操作款违, 前期可以使用一個(gè)全局的標(biāo)志位來標(biāo)志線程的退出唐瀑。
說白了就是: 如果線程中有while, 需要打破while循環(huán),用以達(dá)到退出效果
import _thread
import utime
counter = 0
def led_func():
global counter
while counter<20:
print("這是一個(gè)LED閃燈程序, 我在每2秒瘋狂閃燈!進(jìn)程號(hào)",_thread.get_ident())
utime.sleep(2)
def add1_persecond():
while True:
global counter
counter += 1
print("計(jì)數(shù)加一:",counter,",進(jìn)程號(hào):",_thread.get_ident())
utime.sleep(1)
if __name__ == "__main__":
_thread.start_new_thread(led_func,())
_thread.start_new_thread(add1_persecond,())
二. uio - 輸入輸出流
uio 模塊包含其他類型的stream(類文件)對(duì)象和輔助函數(shù)。
import uio
1. 打開文件
fd = uio.open(name, mode=’r’, **kwarg)
參數(shù):
- name :文件名
- mode :打開模式 r : 只讀模式打開文件, w 寫入模式打開文件插爹,每次寫入會(huì)覆蓋上次寫入數(shù)據(jù),a 只寫追加模式打開文件哄辣,可連續(xù)寫入文件數(shù)據(jù)而不是覆蓋數(shù)據(jù)
- **kwarg:可變長(zhǎng)參數(shù)列表
2. 關(guān)閉打開的文件
fd.close()
我們得到的文檔流該怎么操作呢?? 詳見: 第三節(jié) : 文件和操作系統(tǒng)
三. 文件操作
1. read() 讀文件對(duì)象
import uio
file = uio.open("/usr/test.txt",'r')
txt = file.read()
print(txt)
2. write() 寫文件對(duì)象
寫文件對(duì)象, 有 w 和 a 兩種模式, 返回寫入的字節(jié)數(shù)
import uio
file = uio.open("/usr/test.txt",'a')
file.write("好詩(shī),好詩(shī)呀!")
print(txt)
四. gc - 內(nèi)存碎片回收
gc 模塊實(shí)現(xiàn)內(nèi)存垃圾回收機(jī)制请梢,該模塊實(shí)現(xiàn)了CPython模塊相應(yīng)模塊的子集。
import gc
方法 | 描述 |
---|---|
gc.enable() | 啟用自動(dòng)回收內(nèi)存碎片機(jī)制 |
gc.disable() | 禁用自動(dòng)回收機(jī)制 |
gc.collect() | 回收內(nèi)存碎片 |
gc.mem_alloc() | 返回分配的堆RAM的字節(jié)數(shù)力穗。此功能是MicroPython擴(kuò)展 |
gc.mem_free() | 返回可用堆RAM的字節(jié)數(shù)毅弧,如果此數(shù)量未知,則返回-1当窗。此功能是MicroPython擴(kuò)展 |