python线程里哪种模块好

发布时间:2020-08-03 09:09:48 作者:小猪
来源:亿速云 阅读:155

小编这次要给大家分享的是python线程里哪种模块好,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

在Python中可使用的多线程模块主要有两个,thread和threading模块。thread模块提供了基本的线程和锁的支持,建议新手不要使用。threading模块允许创建和管理线程,提供了更多的同步原语。

thread模块函数:

内容扩展:

Python线程模块

常用参数说明

常用的方法

import time
from threading import Thread
 
 
def hello(name):
  print('hello {}'.format(name))
  time.sleep(3)
  print('hello bye')
 
def hi():
  print('hi')
  time.sleep(3)
  print('hi bye')
 
if __name__ == '__main__':
 
  hello_thread = Thread(target=hello, args=('wan zong',),name='helloname') #target表示调用对象。name是子线程的名称。args 传入target函数中的位置参数,是个元组,参数后必须加逗号
  hi_thread = Thread(target=hi)
 
  hello_thread.start() #开始执行线程任务,启动进程
  hi_thread.start()
 
  hello_thread.join() #阻塞进程 等到进程运行完成 阻塞调用,主线程进行等待
  hi_thread.join()
 
  print(hello_thread.getName())
  print(hi_thread.getName()) #会默认匹配名字
 
  hi_thread.setName('hiname')
  print(hi_thread.getName())
 
  print('主线程运行完成!')

看完这篇关于python线程里哪种模块好的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

推荐阅读:
  1. 多线程 threading模块___python
  2. 哪种类型的云主机好

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:python的证书有哪些

下一篇:笔记 综合布线、子网划分

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》