<pre class="brush:python;gutter:true;">import time
import random
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Process, Pool
def worker(n, index):
print('开始第{}个进程,第{}个线程'.format(n, index))
t = random.random()
time.sleep(t)
print('结束第{}个进程,第{}个线程'.format(n, index))
def main(n):
max_workers = 20 # 最大线程数
pool = ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix='Thread')
i = 0
while True:
pool.submit(worker, n, i)
i = i + 1
if __name__ == "__main__":
pool1 = Pool(2) # 最大进程数2
for i in range(1,3):
pool1.apply_async(main, args=(i, ))
pool1.close()
pool1.join()