main idea
Time for space
Process pool
Process pool : a container, the container limits on the quantity you open the process, the default is os.cpu_count (), my computer is an eight-core, they are able to open eight, eight processing task is certainly the first time only in parallel, as long as the task is completed, the process will soon take over a task.
Code:
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
import os,time,random
# print(os.cpu_count())
def task(n):
print(f"{os.getpid()} 接客")
time.sleep(random.randint(1,3))
if __name__ == '__main__':
p = ProcessPoolExecutor()
for i in range(30):
p.submit(task,1)
Thread Pool
Thread Pool : A thread can perform up to five times the process, which is 40
Code:
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
import os,time,random
# print(os.cpu_count())
def task(n):
print(f"{os.getpid()} 接客")
time.sleep(random.randint(1,3))
if __name__ == '__main__':
# p = ProcessPoolExecutor()
# for i in range(30):
# p.submit(task,1)
t = ThreadPoolExecutor()
for i in range(200):
t.submit(task,i)