python3 多线程的实现
发布日期:2021-07-01 04:02:03 浏览次数:2 分类:技术文章

本文共 862 字,大约阅读时间需要 2 分钟。

1. _thread方法

_thread 提供了低级别的、原始的线程以及一个简单的锁

import _threadimport time# 定义线程函数1def print_test1():    while 1:        print("我是子线程1")        time.sleep(1)    def print_test2(argsValue):    while 1:        print(argsValue)        time.sleep(1)try:    _thread.start_new_thread(print_test1,())    _thread.start_new_thread(print_test2,("我是子线程2",))except:    print("ERROR!")while 1:    print("我是主线程")    time.sleep(1)

2. threading模块法

threading 模块除了包含 _thread 模块中的所有方法,此外还包括:

  • threading.currentThread(): 返回当前的线程变量。
  • threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
  • threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:

  • run(): 用以表示线程活动的方法。
  • start():启动线程活动。
  • join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
  • isAlive(): 返回线程是否活动的。
  • getName(): 返回线程名。
  • setName(): 设置线程名。

参考文献:

转载地址:https://miracle.blog.csdn.net/article/details/107751917 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:关于深度强化学习的一点记录
下一篇:【转】傅里叶分析之掐死教程

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月10日 08时16分06秒