python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python线程用什么模块好

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

作者:yang

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

您可能感兴趣的文章:
阅读全文