Last active
August 29, 2015 14:24
-
-
Save ohe/e210fc5374673f3fc15b to your computer and use it in GitHub Desktop.
GIL + Cython + Subprocess freeze
This file contains hidden or 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 test import zero | |
from functools import wraps | |
from multiprocessing import Process, Queue | |
def queue_result(function, queue): | |
@wraps(function) | |
def decorated_function(*args, **kwargs): | |
has_raised = False | |
try: | |
to_ret = function(*args, **kwargs) | |
except Exception as e: | |
has_raised = True | |
to_ret = str(e) | |
queue.put(to_ret) | |
queue.put(has_raised) | |
return decorated_function | |
def run_in_subprocess(function): | |
@wraps(function) | |
def decorated_function(*args, **kwargs): | |
queue = Queue() | |
process = Process(target=queue_result(function, queue), | |
args=args, kwargs=kwargs) | |
process.start() | |
process.join() | |
to_ret = queue.get() | |
has_raised = queue.get() | |
if has_raised: | |
raise Exception(to_ret) | |
else: | |
return to_ret | |
return decorated_function | |
def zero_in_subprocess(in_parallel): | |
@run_in_subprocess | |
def sub_test(): | |
zero(10, in_parallel) | |
return 1 | |
print zero(10, in_parallel) | |
print [sub_test() for _ in range(10)] | |
zero_in_subprocess(False) # it doesn't freeze | |
zero_in_subprocess(True) # it freezes | |
# Expected results would be: | |
# 0 | |
# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
# 0 | |
# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
# but the last output will never come :( |
This file contains hidden or 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 distutils.core import setup | |
from Cython.Distutils.extension import Extension | |
from Cython.Build import cythonize | |
from Cython.Distutils import build_ext | |
import numpy as np | |
ext_modules = [Extension("test", | |
["test.pyx"], | |
include_dirs=[np.get_include()], | |
language='c++', | |
extra_compile_args=['-fopenmp'], | |
extra_link_args=['-fopenmp'] | |
)] | |
setup( | |
name='test', | |
cmdclass={'build_ext': build_ext}, | |
ext_modules=cythonize(ext_modules), | |
) |
This file contains hidden or 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 cython.parallel import prange | |
def zero(long n=10, in_parallel=False): | |
cdef long i | |
if in_parallel: | |
for i in prange(n, nogil=True): | |
pass | |
else: | |
for i in xrange(n): | |
pass | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment