Multithreading in Python

The package for multithreading

The package threading is for multithreaded programming in python. To get it ready to use, one should import it. As:

import threading

Get your hand dirty

import threading
def add(n):
print('I am working.')
result = n + 1000
return result
thread0 = threading.Thread(target=add, args=(1,))
thread0.start()

This code could be able to run. There is a function add(n) defined. Then, a thread was created to run the function. The argument, target, tells the thread which function is going to be run. The argument, args, tells the thread what is the arguments fed the target function.
NOTE: The fed value for args of the thread must be a tuple. So, if you’re going to feed the function just one value, the comma is also needed.

How to get the value returned

import threading
class MyThread(threading.Thread):
def __init__(self,func,args=()):
super(MyThread,self).__init__()
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
try:
return self.result # 如果子线程不使用join方法,此处可能会报没有self.result的错误
except Exception:
return None
def add(n):
print('I am working.')
result = n + 1000
return result
################
thrdList = []
for i in range(4):
t = MyThread(add, args=(i,))
thrdList.append(t)
t.start()
reslutList = []
for t in thrdList:
t.join()
reslutList.append(t.get_result())
print(reslutList)

output:
这里写图片描述
t.join(): It could be interpreted as wait_until_finished(t). Whitout this sentence, the thread may just have done half of its job, the main thread read its result. In this scenario, the main thread read a wrong result.

REF:
multithreading - what is the use of join() in python threading - Stack Overflow
https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading
python multithreading wait till all threads finished - Stack Overflow
https://stackoverflow.com/questions/11968689/python-multithreading-wait-till-all-threads-finished
python - Use of threading.Thread.join() - Stack Overflow
https://stackoverflow.com/questions/19138219/use-of-threading-thread-join

Reference:

python获取多线程的返回值 - Hu知非 - 博客园
https://www.cnblogs.com/hujq1029/p/7219163.html?utm_source=itdadao&utm_medium=referral
multithreading - what is the use of join() in python threading - Stack Overflow
https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading
python multithreading wait till all threads finished - Stack Overflow
https://stackoverflow.com/questions/11968689/python-multithreading-wait-till-all-threads-finished
python - Use of threading.Thread.join() - Stack Overflow
https://stackoverflow.com/questions/19138219/use-of-threading-thread-join

posted on   yusisc  阅读(22)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示