|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 MSK 于 2017-6-10 20:15 编辑
上一篇:
高级锁
内容概括
拾遗
threading模块几乎已经讲解完了,还剩下一些小小的函数,也很有用!
threading.Timer()
可以这么玩:
- import threading
- def say_hello():
- print("hello, world")
-
- t = threading.Timer(3,say_hello)
- t.start() # 3秒钟之后执行hello函数。
复制代码
threading.active_count()
threading.activeCount()
获取当前活动的(alive)线程的个数。
threading.current_thread()
threading.currentThread()
获取当前的线程对象(Thread object)
- import threading
- def say_hi():
- print('hi\n')
- [u][/u]
- def say_hello():
- print('hello\n')
- #这样main函数就相当于大脑了
- def main():
- if threading.current_thread().name == '线程1':
- say_hi()
- elif threading.current_thread().name == '线程2':
- say_hello()
- threading.Thread(target=main,name='线程1').start()
- threading.Thread(target=main,name='线程2').start()
复制代码
threading.enumerate()
获取当前所有活动线程的列表
threading.settrace(func)
设置一个跟踪函数,用于在run()执行之前被调用
threading.setprofile(func)
设置一个跟踪函数,用于在run()执行完毕之后调用
|
评分
-
查看全部评分
|