鱼C论坛

 找回密码
 立即注册
查看: 4007|回复: 3

[技术交流] python标准库之【threading】 第6讲

[复制链接]
发表于 2017-6-7 17:51:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 MSK 于 2017-6-10 20:15 编辑

上一篇:
        高级锁

内容概括
拾遗



        threading模块几乎已经讲解完了,还剩下一些小小的函数,也很有用!
       

        threading.Timer()
        可以这么玩:
  1. import threading
  2. def say_hello():
  3.     print("hello, world")
  4.    
  5. t = threading.Timer(3,say_hello)
  6. t.start() # 3秒钟之后执行hello函数。
复制代码



        threading.active_count()
        threading.activeCount()

        获取当前活动的(alive)线程的个数。


        threading.current_thread()
        threading.currentThread()

        获取当前的线程对象(Thread object)
       

  1. import threading

  2. def say_hi():
  3.     print('hi\n')
  4. [u][/u]
  5. def say_hello():
  6.     print('hello\n')

  7. #这样main函数就相当于大脑了
  8. def main():
  9.     if threading.current_thread().name == '线程1':
  10.         say_hi()
  11.     elif threading.current_thread().name == '线程2':
  12.         say_hello()

  13. threading.Thread(target=main,name='线程1').start()
  14. threading.Thread(target=main,name='线程2').start()
复制代码



        threading.enumerate()
        获取当前所有活动线程的列表


        threading.settrace(func)
        设置一个跟踪函数,用于在run()执行之前被调用


        threading.setprofile(func)
        设置一个跟踪函数,用于在run()执行完毕之后调用
       
       
       
c69d574de5c648c51784383f4f1bf8db.jpg

评分

参与人数 2鱼币 +4 收起 理由
康小泡 + 2
小甲鱼 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-28 11:47:08 | 显示全部楼层
排版不是很美,如果每个函数都有一个例子会更棒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-11 17:09:41 | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-8 10:10:42 | 显示全部楼层
棒棒哒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 10:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表