Python如何标识线程?

571次阅读
没有评论

Python如何标识线程?

1、threading.current_thread() 可以返回当前的线程对象。

>>> threading.Thread(target=lambda: print(threading.current_thread())).start()
<Thread(Thread-13, started 140007299499776)>

返回的线程对象我们可以通过一个变量进行接收:

thread = threading.current_thread()

2、属性和方法

name:返回线程的名字;

ident:返回该线程的唯一标识符;

is_alive:告知该线程是否存活;

enumerate:可以通过循环它打印出所有的线程;

3、实例

我们创建线程对象的时候是可以给它取名字的:

t = threading.Thread(target=worker, name='thread1')

这个name可以通过logging的threadName获得。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-30发表,共计409字。
新手QQ群:570568346,欢迎进群讨论 Python51学习