首页 > 编程技术 > python

python线程里哪种模块比较适合

发布时间:2020-8-2 12:13

在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线程里哪种模块比较适合的文章就介绍到这了,更多相关python线程用什么模块好内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

标签:[!--infotagslink--]

您可能感兴趣的文章: