python threading.Thread 传参
时间: 2023-08-17 10:06:50 浏览: 188
python函数传参
5星 · 资源好评率100%
在Python中使用threading模块创建线程时,可以使用threading.Thread类的构造函数来传递参数。具体方法是在创建threading.Thread实例时,将需要传递的参数作为关键字参数传入构造函数中。例如:
```python
import threading
def my_function(arg1, arg2):
# do something with arg1 and arg2
print(arg1, arg2)
thread = threading.Thread(target=my_function, kwargs={'arg1': 'hello', 'arg2': 'world'})
thread.start()
```
在上面的代码中,我们将arg1和arg2作为关键字参数传递给了threading.Thread构造函数,这样在调用线程的start()方法时,就可以将这些参数传递给my_function函数。\[1\]
#### 引用[.reference_title]
- *1* [python threading.Thread如何传递参数](https://blog.csdn.net/weixin_35755188/article/details/128867851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [Python多任务(多线程执行带有参数的任务,利用threading创建线程时传入参数--args参数和kwargs参数)](https://blog.csdn.net/wei18791957243/article/details/83995823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文