python多进程笔记

import multiprocessing
import datetime

def calc(i):
sum =0
for _ in range(1000000):
sum +=1

if __name__ == "__main__":
start =datetime.datetime.now()
lst = []
for i in range(10):
p =multiprocessing.Process(target=calc,args=(i,),name="{}-{}".format(multiprocessing.current_process().name,i))
p.start()
lst.append(p)

for i in lst:
i.join()

detail = (datetime.datetime.now() - start).total_seconds()
print(detail)

---------------------------------------------------------------
import multiprocessing
import datetime

def calc(i):
sum =0
for _ in range(1000000):
sum +=1

if __name__ == "__main__":
start =datetime.datetime.now()
pool = multiprocessing.Pool(5)

for i in range(5):
pool.apply_async(calc,args=(i,))

pool.close()
pool.join()
detail = (datetime.datetime.now() - start).total_seconds()
print(detail)

----------------------------------------------------------------
import threading
import multiprocessing
import time
from concurrent import futures
import logging
FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO,format=FORMAT)



def worker(i):
sum =0
time.sleep(1)
logging.info("worker-{}".format(i))
for _ in range(10000):
sum +=1

exec =futures.ThreadPoolExecutor(max_workers=3)

fs = []

for i in range(3):
f =exec.submit(worker,i)
fs.append(f)

for i in range(3,6):
f =exec.submit(worker,i)
fs.append(f)

while True:
time.sleep(2)
logging.info(threading.enumerate())
flag = True
for f in fs:
flag = flag and f.done()
if flag:
exec.shutdown()
break
------------------------------------------
import threading
import multiprocessing
import time
from concurrent import futures
import logging
FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO,format=FORMAT)



def worker(i):
sum =0
time.sleep(1)
logging.info("worker-{}".format(i))
for _ in range(10000):
sum +=1

if __name__ == "__main__":
exec =futures.ProcessPoolExecutor(max_workers=3)

fs = []

for i in range(3):
f =exec.submit(worker,i)
fs.append(f)

for i in range(3,6):
f =exec.submit(worker,i)
fs.append(f)

while True:
time.sleep(2)
#logging.info(threading.enumerate())
flag = True
for f in fs:
flag = flag and f.done()
if flag:
exec.shutdown()
break
---------------------------------------------------
import threading
import multiprocessing
import time
from concurrent import futures
import logging
FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO,format=FORMAT)


def worker(i):
sum =0
time.sleep(1)
logging.info("worker-{}".format(i))
for _ in range(10000):
sum +=1
logging.info(multiprocessing.current_process().name)

if __name__ == "__main__":
fs = []
with futures.ProcessPoolExecutor(max_workers=4) as exec:
for i in range(3):
f = exec.submit(worker,i)
fs.append(f)

for i in range(3,6):
f =exec.submit(worker,i)
fs.append(f)

while True:
time.sleep(0.1)
flag = True
for f in fs:
flag = flag and f.done()
if flag:
exec.shutdown()
break

posted @ 2020-02-02 17:08  冬日的温暖  阅读(169)  评论(0编辑  收藏  举报