python多线程threading模块使用技巧

281次阅读
没有评论

python多线程threading模块使用技巧

任何一个区域设定里总归是有一个掌控大局的管理者,这跟我们在公司里,需要一个领导统筹布局是一样的道理,那在python多线程里,也有一个这么重要角色的方法——threading,相信大家也不少见过吧,那大家知道关于这个方法实用的功能有哪些吗?为什么大家都选择它?还理解认知不清楚的,可以继续往下看文。

python多线程threading模块使用技巧:

多线程启动

# 多线程启动
import os
import time
from threading import Thread
 
def func():
    time.sleep(1)
    print('hello 线程', os.getpid())
 
t = Thread(target=func)
t.start()
print(os.getpid())
 
# 结果
# 6360
# hello 线程 6360

同步开启多线程

# 同步开启多线程
import os
import time
from threading import Thread
 
def func():
    time.sleep(1)
    print('hello 线程', os.getpid())
thread_l = []
for i in range(10):
    t = Thread(target=func)
    t.start()
    thread_l.append(t)
for j in thread_l:
    j.join()
print(os.getpid())

大家如果在碰到需要多线程运转开启的时候,直接调用这个模块,演示代码在上述给大家均已提供,如果还需要代码解说,直接套用上述内容即可。

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

相关文章:

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