Multithreading in Python
来源:互联网 发布:苹果6网络通信出现问题 编辑:程序博客网 时间:2024/05/22 07:59
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 threadingdef add(n): print('I am working.') result = n + 1000 return resultthread0 = 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 threadingclass 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 Nonedef 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
- Multithreading in Python
- Multithreading in .NET Applications
- Multithreading in Cocoa
- Multithreading in Cocoa
- Multithreading in Cocoa
- Python Multithreading 例子
- Python multithreading programming
- Multithreading in C and Java
- Multithreading Applications in Win32 Faqs
- Multithreading in swing with SwingWorker
- python学习之路--multithreading
- multithreading
- Cooperative Multithreading in BREW with IThread
- Simpler Multithreading in C++0x
- Android – Multithreading in a UI environment
- iOS Multithreading: Thread Safety in iOS Applications
- Android – Multithreading in a UI environment
- Java - Race condition in Java multithreading
- UVA10548+10717
- async/await
- 数据结构实验之二叉树二:遍历二叉树
- hdu 2070
- SSM三大框架整合配置(2)-----Spring+Mybatis
- Multithreading in Python
- 07 C++模板特化、模板偏特化和模板模板参数基本用法(学自Boolean)
- vue.js_v-model笔记
- async/await
- ASP.NET MVC DropDownList的使用
- cenos系统python项目报错mysql Access denied for user root@localhost错误解决方法
- 深夜博客
- C++标准库中string类型与数值类型之间的转换
- Spring声明式事务注意点,以及不生效情况