1 Star 0 Fork 0

读梦人/python_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
068-2-前期作业回顾--线程进程互相嵌套.py 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
读梦人 提交于 2023-08-30 23:38 . 作业 基础爬虫、aiohttp+携程
#之前的作业回顾:
#1)多线程中的线程任务,是否可以开启新的线程或进程
#2)多进程中的进程任务,是否可以开启新的线程或进程
import os
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
def task():
print(task.__module__, task.__name__)
pid = os.getpid()
print(f'This is task, pid is {pid}')
time.sleep(1)
def create_thread_pool():
with ThreadPoolExecutor(20) as executor:
executor.submit(task)
def create_process_pool():
with ProcessPoolExecutor(7) as executor:
executor.submit(task)
###进程池创建线程
def test1_process_create_thread_pool():
with ProcessPoolExecutor(7) as executor:
executor.submit(create_thread_pool)
###进程池创建进程
def test2_process_create_thread_pool():
with ProcessPoolExecutor(7) as executor:
executor.submit(create_process_pool)
###线程池创建线程
def test3_thread_create_thread_pool():
with ThreadPoolExecutor(20) as executor:
executor.submit(create_thread_pool)
###线程池创建进程
def test4_thread_create_process_pool():
with ThreadPoolExecutor(20) as executor:
executor.submit(create_process_pool)
if __name__ == '__main__':
test1_process_create_thread_pool()
test2_process_create_thread_pool()
test3_thread_create_thread_pool()
test4_thread_create_process_pool()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/w854090472/python_study.git
git@gitee.com:w854090472/python_study.git
w854090472
python_study
python_study
master

搜索帮助