2.24 haas506 2.0开发教程 - _thread - 多线程

最新案例教程点击下方链接跳转,CSDN已停止更新

点击跳转HaaS506官方最新案例







开启关闭线程

(1)案例说明

  • _thread 模块提供创建新线程的方法,并提供互斥锁。
  • 案例演示如何创建并关闭线程

(2)案例代码

import utime as time
import _thread

def th():
  for i in range (5):
      print('---  new_thread  ---')
      time.sleep_ms(1000)
  #循环结束
  print('_thread.exit')
  _thread.exit()
  

if __name__ == '__main__':
  _thread.start_new_thread(th, ())  #开启gps线程
  while True:
    print('===  main  ===')
    time.sleep_ms(1000)


(3)输出log

  • 线程开启5秒后关闭
    在这里插入图片描述

class - _thread

class - _thread

get_identstack_sizestart_new_threadexitallocate_lock
获取当前线程ID设置创建新线程时所使用的栈大小创建一个新线程结束线程创建一个互斥锁对象
get_ident - 获取当前线程ID

函数原型:

  • _thread.get_ident()

返回值:

  • 获取当前线程号。
stack_size - 设置创建新线程时所使用的栈大小

函数原型:

  • _thread.stack_size(size)

参数说明:

参数类型必选参数?说明
sizeint创建新线程使用的栈大小(以字节为单位),默认为8448字节,最小8192字节。
start_new_thread - 创建一个新线程

函数原型:

  • _thread.start_new_thread(function, args)

参数说明:

参数类型必选参数?说明
function函数指针新线程的入口函数
args元组传入新线程的入口函数的参数列表

返回值:

  • 返回线程的id。
exit - 结束线程

函数原型:

  • _thread.exit ()
allocate_lock - 创建一个互斥锁对象

函数原型:

  • _thread.allocate_lock()

返回值:

  • 返回互斥锁对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值