美文网首页
移远QuecPython(基于EC600s)开发物联网应用(六)

移远QuecPython(基于EC600s)开发物联网应用(六)

作者: 熊爸天下_56c7 | 来源:发表于2021-05-14 08:52 被阅读0次

    一. _thread - 多线程

    _thread 模块提供创建新线程的方法,并提供互斥锁, 线程最多16个

    import _thread
    
    方法 描述
    _thread.get_ident() 获取当前线程号
    _thread.get_heap_size() 获取系统剩余内存大小
    _thread.stack_size(size) 设置创建新线程使用的栈大小(以字节为单位),默认为8k。
    _thread.start_new_thread(function, args) 创建一个新线程,接收执行函数和被执行函数参数,注意:传参要传元组, 当 function 函数无参时传入空的元组。
    _thread.allocate_lock() 创建一个互斥锁对象。
    lock.acquire() 获取锁,成功返回True,否则返回False。
    lock.release() 释放锁
    lock.locked() 返回锁的状态,True表示被某个线程获取,False则表示没有.

    1. 举例一: 简单的多线程并行

    import _thread
    import utime
    
    counter = 0
    
    def led_func():
      while True:
        print("这是一个LED闪灯程序, 我在每2秒疯狂闪灯!进程号",_thread.get_ident())
        utime.sleep(2)
    
    def add1_persecond():
      while True:
        global counter
        counter += 1
        print("计数加一:",counter,",进程号:",_thread.get_ident())
        utime.sleep(1)
      
    
    if __name__ == "__main__":
      _thread.start_new_thread(led_func,())
      _thread.start_new_thread(add1_persecond,())
    

    2. 带参数的进程创建

    带参数创建进程要注意: 参数必须传元组, 所以如果你只有一个参数a, 也应该包裹在元组中 (a,), 如果不需要参数则传递空元组()

    import _thread
    import utime
    
    counter = 100
    
    def add_thread(step):
      while True:
        global counter
        counter += step
        print("计数:",counter,",进程号:",_thread.get_ident())
        utime.sleep(1)
        
    if __name__ == "__main__":
      _thread.start_new_thread(add_thread,(3,))
    

    3. 举例三: 互斥锁

    add10_persecond的for循环操作counter时, 避免minus5_persecond的for循环操作它, 所以加个互斥锁, 每个进程, 拿到这个锁才能操作公共变量counter

    import _thread
    import utime
    
    lock = _thread.allocate_lock() #创建一个互斥锁
    
    counter = 0
    
    def led_func():
      while True:
        print("这是一个LED闪灯程序, 我在每2秒疯狂闪灯!进程号",_thread.get_ident())
        utime.sleep(2)
    
    def add10_persecond():
      while True:
        lock.acquire()
        global counter
        for i in range(10):
          counter += 1
          print("计数加一:",counter,",进程号:",_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("计数减一:",counter,",进程号:",_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. 线程退出

    暂时没有提供类似于 join 函数之类的操作, 前期可以使用一个全局的标志位来标志线程的退出。
    说白了就是: 如果线程中有while, 需要打破while循环,用以达到退出效果

    import _thread
    import utime
    
    counter = 0
    
    def led_func():
      global counter
      while counter<20:
        print("这是一个LED闪灯程序, 我在每2秒疯狂闪灯!进程号",_thread.get_ident())
        utime.sleep(2)
    
    def add1_persecond():
      while True:
        global counter
        counter += 1
        print("计数加一:",counter,",进程号:",_thread.get_ident())
        utime.sleep(1)
    
    if __name__ == "__main__":
      _thread.start_new_thread(led_func,())
      _thread.start_new_thread(add1_persecond,())
    

    二. uio - 输入输出流

    uio 模块包含其他类型的stream(类文件)对象和辅助函数。

    import uio
    

    1. 打开文件

    fd = uio.open(name, mode=’r’, **kwarg)
    

    参数:

    • name :文件名
    • mode :打开模式 r : 只读模式打开文件, w 写入模式打开文件,每次写入会覆盖上次写入数据,a 只写追加模式打开文件,可连续写入文件数据而不是覆盖数据
    • **kwarg:可变长参数列表

    2. 关闭打开的文件

    fd.close()
    

    我们得到的文档流该怎么操作呢?? 详见: 第三节 : 文件和操作系统

    三. 文件操作

    1. read() 读文件对象

    import uio
    file = uio.open("/usr/test.txt",'r')
    txt = file.read()
    print(txt)
    

    2. write() 写文件对象

    写文件对象, 有 w 和 a 两种模式, 返回写入的字节数

    import uio
    file = uio.open("/usr/test.txt",'a')
    file.write("好诗,好诗呀!")
    print(txt)
    

    四. gc - 内存碎片回收

    gc 模块实现内存垃圾回收机制,该模块实现了CPython模块相应模块的子集。

    import gc
    
    方法 描述
    gc.enable() 启用自动回收内存碎片机制
    gc.disable() 禁用自动回收机制
    gc.collect() 回收内存碎片
    gc.mem_alloc() 返回分配的堆RAM的字节数。此功能是MicroPython扩展
    gc.mem_free() 返回可用堆RAM的字节数,如果此数量未知,则返回-1。此功能是MicroPython扩展

    相关文章

      网友评论

          本文标题:移远QuecPython(基于EC600s)开发物联网应用(六)

          本文链接:https://www.haomeiwen.com/subject/qbptjltx.html