import multiprocessing
import os
import time
deffunc(name,id):print(name,'pid:',os.getpid(),"id:",id)
time.sleep(3)print("end")if __name__ =="__main__":foridinrange(10):
process = multiprocessing.Process(target=func, args=('process',id))
process.start()
record.append(process)for process in record:
process.join()
多进程池
import multiprocessing
import os
import time
deffunc(name,id):print(name,'pid:',os.getpid(),"id:",id)
time.sleep(3)print("end")if __name__ =="__main__":
pool = multiprocessing.Pool(processes=10)foridinrange(10):
pool.apply_async(func,args=('process',id))
pool.close()
pool.join()
多线程
import threading
import os
import time
deffunc(name,id):print(name,'pid:',os.getpid(),"id:",id)
time.sleep(3)print("end")if __name__ =="__main__":foridinrange(10):
thread = threading.Thread(target=func, args=('process',id))
thread.start()
record.append(thread)for thread in record:
thread.join()
多线程池
import threadpool
import os
import time
deffunc(id):print('pid:',os.getpid(),"id:",id)
time.sleep(3)print("end")if __name__ =="__main__":
pool = threadpool.ThreadPool(10)
requests = threadpool.makeRequests(func,range(10))for request in requests:
pool.putRequest(request)
pool.wait()