Skip to content

Instantly share code, notes, and snippets.

@DmitryUlyanov
Created January 31, 2017 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DmitryUlyanov/b2040ab673c0ec9186eac9a0b336d9ea to your computer and use it in GitHub Desktop.
Save DmitryUlyanov/b2040ab673c0ec9186eac9a0b336d9ea to your computer and use it in GitHub Desktop.
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