| 1 |
# Monkey patching interferes with threading in Python 3.7 |
| 2 |
# https://github.com/eventlet/eventlet/issues/592 |
| 3 |
__test__ = False |
| 4 |
|
| 5 |
|
| 6 |
def check(n, mod, tag): |
| 7 |
assert len(mod._active) == n, 'Expected {} {} threads, got {}'.format(n, tag, mod._active) |
| 8 |
|
| 9 |
|
| 10 |
if __name__ == '__main__': |
| 11 |
import eventlet |
| 12 |
import eventlet.patcher |
| 13 |
eventlet.monkey_patch() |
| 14 |
import os |
| 15 |
import sys |
| 16 |
import threading |
| 17 |
_threading = eventlet.patcher.original('threading') |
| 18 |
import eventlet.green.threading |
| 19 |
|
| 20 |
global threads_keep_running |
| 21 |
threads_keep_running = True |
| 22 |
|
| 23 |
def target(): |
| 24 |
while threads_keep_running: |
| 25 |
eventlet.sleep(0.001) |
| 26 |
|
| 27 |
threads = [ |
| 28 |
threading.Thread(target=target, name='patched'), |
| 29 |
_threading.Thread(target=target, name='original-1'), |
| 30 |
_threading.Thread(target=target, name='original-2'), |
| 31 |
eventlet.green.threading.Thread(target=target, name='green-1'), |
| 32 |
eventlet.green.threading.Thread(target=target, name='green-2'), |
| 33 |
eventlet.green.threading.Thread(target=target, name='green-3'), |
| 34 |
] |
| 35 |
for t in threads: |
| 36 |
t.start() |
| 37 |
|
| 38 |
check(5, threading, 'pre-fork patched') |
| 39 |
check(3, _threading, 'pre-fork original') |
| 40 |
check(5, eventlet.green.threading, 'pre-fork green') |
| 41 |
|
| 42 |
pid = os.fork() |
| 43 |
if pid == 0: |
| 44 |
# Inside the child, we should only have a main _OS_ thread, |
| 45 |
# but green threads should survive. |
| 46 |
check(5, threading, 'child post-fork patched') |
| 47 |
check(1, _threading, 'child post-fork original') |
| 48 |
check(5, eventlet.green.threading, 'child post-fork green') |
| 49 |
threads_keep_running = False |
| 50 |
sys.exit() |
| 51 |
else: |
| 52 |
wait_pid, status = os.wait() |
| 53 |
exit_code = os.waitstatus_to_exitcode(status) |
| 54 |
assert wait_pid == pid |
| 55 |
assert exit_code == 0, exit_code |
| 56 |
|
| 57 |
# We're in the parent now; all threads should survive: |
| 58 |
check(5, threading, 'post-fork patched') |
| 59 |
check(3, _threading, 'post-fork original') |
| 60 |
check(5, eventlet.green.threading, 'post-fork green') |
| 61 |
|
| 62 |
threads_keep_running = False |
| 63 |
|
| 64 |
for t in threads: |
| 65 |
t.join() |
| 66 |
|
| 67 |
check(1, threading, 'post-join patched') |
| 68 |
check(1, _threading, 'post-join original') |
| 69 |
check(1, eventlet.green.threading, 'post-join green') |
| 70 |
print('pass') |