Skip to content

Instantly share code, notes, and snippets.

@TakesxiSximada
Last active October 13, 2016 12:14
Show Gist options
  • Save TakesxiSximada/f9569554e7d525044c746613bc8e9a27 to your computer and use it in GitHub Desktop.
Save TakesxiSximada/f9569554e7d525044c746613bc8e9a27 to your computer and use it in GitHub Desktop.
concurrent.futures

concurrent.futures の使い方

ただの単純な使い方

$ python run.py
OK: 1
OK: 0
OK: 3
OK: 6
OK: 4
OK: 8
OK: 2
OK: 7
OK: 5
OK: 9
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
$



#! /usr/bin/env python
import time
from concurrent.futures import ThreadPoolExecutor as Executor
def publish(num):
time.sleep(5)
print('OK: {}'.format(num))
return 1
def finish(*args, **kwds):
print('finish: {} {}'.format(args, kwds))
def get_devices():
for ii in range(10):
yield ii
with Executor(max_workers=200) as executor:
for result in executor.map(publish, get_devices()):
print(type(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment