Created
January 31, 2017 08:24
-
-
Save DmitryUlyanov/b2040ab673c0ec9186eac9a0b336d9ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import threading | |
from joblib import Parallel, delayed | |
import Queue | |
# Fix print | |
_print = print | |
_rlock = threading.RLock() | |
def print(*args, **kwargs): | |
with _rlock: | |
_print(*args, **kwargs) | |
N_GPU = 4 | |
q = Queue.Queue(maxsize=N_GPU) | |
for i in range(N_GPU): | |
q.put(i) | |
def runner(x): | |
gpu = q.get() | |
print(gpu) | |
q.put(gpu) | |
Parallel(n_jobs=N_GPU, backend="threading")( | |
delayed(runner)(i) for i in range(8)) | |
# prints: | |
# 0 | |
# 1 | |
# 2 | |
# 3 | |
# 0 | |
# 1 | |
# 2 | |
# 3 | |
# with backend="multiprocessing" | |
# 0 | |
# 0 | |
# 0 | |
# 1 | |
# 0 | |
# 1 | |
# 1 | |
# 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment