Pythonマルチプロセスとマルチスレッド最適化
Pythonマルチプロセスとマルチスレッド最適化
Pythonマルチスレッドコード
Pythonマルチプロセスコード
Pythonスレッドプールとプロセスプール
Pythonマルチスレッドコード
from time import ctime, sleep
import threading
import numpy as np
import collections
loops = [1e6,1e7]
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
self.result = self.func(*self.args)
def get_result(self):
try:
return self.result
except Exception:
return None
def loop(num):
sum = 0
for i in range(1,num+1):
sum = sum + i
return sum
def main():
print('Start:', ctime())
threads = []
nloops = range(len(loops))
for i in nloops:
t = MyThread(loop, (int(loops[i]),), loop.__name__)
threads.append(t)
for i in nloops: # start threads , , 。 start
threads[i].start()
for i in nloops: # jion()
threads[i].join()
for i in nloops: # jion()
print(threads[i].get_result())
print('Done:', ctime())
if __name__ == '__main__':
main()
Pythonマルチプロセスコード
import multiprocessing
import time
def loop(num):
sum = 0
for i in range(1,num+1):
sum = sum + i
return sum
if __name__ == '__main__':
print('Start:', ctime())
pool = multiprocessing.Pool(multiprocessing.cpu_count())
result = []
loops = [1e6,1e7]
for i in range(len(loops)):
result.append(pool.apply_async(loop, (int(loops[i]), )))
pool.close()
pool.join()
for res in result:
print(res.get())
print('Done:', ctime())
Pythonスレッドプールとプロセスプール
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
loops = [1e6,1e7]
def loop(num,num1):
sum = 0
for i in range(1,num+1):
sum = sum + i
return sum
def main():
print('Start:', ctime())
tasks = []
nloops = range(len(loops))
executor = ThreadPoolExecutor(max_workers=8) # ProcessPoolExecutor(8)
for i in nloops:
tasks.append(executor.submit(loop, int(loops[i]),5))
for i in nloops: # start threads , , 。 start
print(tasks[i].result())
print('Done:', ctime())
if __name__ == '__main__':
main()