当前位置:主页 > >

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

时间:2020-08-02 13:00:05 | 栏目: | 点击:

在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('主线程运行完成!')

您可能感兴趣的文章:

相关文章