【python3】簡単な並列処理の書き方
pythonで並列処理をした時のメモ。
python 3.7.1にて動作確認
import random
from hashids import Hashids # pip install hashids
from concurrent import futures
import time
import random
def random_sleep(index):
print('start. %s' % index)
sleep_seconds = random.randint(1, 5)
time.sleep(sleep_seconds)
print('end. %s' % index)
if __name__ == '__main__':
# max_workers=8 でコア数を指定できる
# 指定なしはフルパワー稼働
with futures.ThreadPoolExecutor(max_workers=8) as executor:
for i in range(10):
future = executor.submit(fn=random_sleep, index=i)