THREAD_SUB_THREAD
时间: 2023-12-17 08:04:29 浏览: 38
以下是两个Python开辟新线程的例子:
1.使用threading模块开辟新线程
```python
import threading
def myThread():
print('位置1', threading.current_thread().name)
print('位置2', threading.current_thread().name)
# 主线程
print('位置1', threading.current_thread().name)
print('位置2', threading.current_thread().name)
# 开辟新线程
sub_thread = threading.Thread(target=myThread, name='newThread')
sub_thread.start()
# 主线程
print('位置1', threading.current_thread().name)
print('位置2', threading.current_thread().name)
```
输出结果为:
```
位置1 MainThread
位置2 MainThread
位置1 newThread
位置2 newThread
位置1 MainThread
位置2 MainThread
```
2.使用threading.Thread开辟新线程
```python
import threading
def myThread():
print('位置1', threading.current_thread().name)
print('位置2', threading.current_thread().name)
# 开辟新线程
sub_thread = threading.Thread(target=myThread, name='newThread')
sub_thread.start()
# 主线程
print('outside1', threading.current_thread().name)
print('outside2', threading.current_thread().name)
```
输出结果为:
```
位置1 newThread
位置2 newThread
outside1 MainThread
outside2 MainThread
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)