标签:requests sync list sleep pid mutex 进程池 import print
#coding=utf-8 import multiprocessing import threadpool import threading import time import os import random mutex = threading.Lock() def write_to_file(file_name, text): mutex.acquire() a = open(file_name, ‘a‘) a.write(text + "\n") a.close() mutex.release() def test1(msg): t_start = time.time() print("%s开始执行,进程号为%d" % (msg, os.getpid())) time.sleep(random.random() * 2) t_stop = time.time() print("%s执行完成,耗时%.2f" % (msg, t_stop - t_start)) def multithread(parm_list,threading_num): #这里存放的是放入线程池的参数 #放入的内容是列表 #调用函数的参数和列表的内容顺序是一样的 for i in parm_list: password_list.append((i, None)) pool = threadpool.ThreadPool(threading_num) requests_a = threadpool.makeRequests(req, password_list) [pool.putRequest(req) for req in requests_a] pool.wait() def multiprocess(): po = multiprocessing.Pool(3) for i in range(0, 10): # Pool().apply_async(要调用的目标,(传递给目标的参数元祖,)) # 每次循环将会用空闲出来的子进程去调用目标 # for j in range() po.apply_async(test1, (i,)) print("-----start-----") po.close() # 关闭进程池,关闭后po不再接收新的请求 po.join() # 等待po中所有子进程执行完成,必须放在close语句之后 print("-----end-----") def main(): pass
标签:requests sync list sleep pid mutex 进程池 import print
原文地址:https://www.cnblogs.com/yakoazz/p/11109434.html