Created
July 2, 2017 13:44
-
-
Save justdoit0823/5b3eb31bb4f08aba42bbd56ec1a0a552 to your computer and use it in GitHub Desktop.
Deadlock in python after fork
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
import multiprocessing | |
import os | |
import threading | |
import time | |
def child_thread(lock): | |
with lock: | |
time.sleep(10) | |
print('work done.') | |
def child_worker(lock): | |
print('run child worker..') | |
with lock: | |
print('got lock...') | |
print('child worker exit.') | |
def run_fork(): | |
lock = threading.Lock() | |
child_t = threading.Thread(target=child_thread, args=(lock,)) | |
child_t.start() | |
time.sleep(3) | |
print('main thread identity in parent process', threading.get_ident()) | |
pid = os.fork() | |
if pid == -1: | |
raise RuntimeError('fork failed.') | |
if pid == 0: | |
print('main thread identity in child process', threading.get_ident()) | |
child_worker(lock) | |
else: | |
time.sleep(3600) | |
def main(): | |
run_fork() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment