Created
November 15, 2019 19:44
-
-
Save mfm24/e62ec5d50c672524107ca00a391e6104 to your computer and use it in GitHub Desktop.
Multiprocessing Logging Deadlock
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
# See eg https://codewithoutrules.com/2018/09/04/python-multiprocessing/ | |
# this deadlocks (Process p never terminates) | |
import logging | |
import multiprocessing | |
import threading | |
import time | |
class SlowHandler(logging.Handler): | |
""" Waits 1 second, then prints """ | |
def emit(self, record): | |
time.sleep(1) | |
print(record) | |
# test | |
log = logging.getLogger(__name__) | |
log.addHandler(SlowHandler()) | |
# Thread function | |
def thread_a(): | |
log.warn("Hi there") | |
# Process function | |
def proc_a(): | |
print("Starting process") | |
log.warn("What's up?") | |
print("Process complete!") | |
# Start thread | |
t = threading.Thread(target=thread_a) | |
t.start() | |
# Wait half a second. Thread should be in the middle of calling the SlowHandler | |
time.sleep(0.5) | |
# Start process while in SlowHandler | |
p = multiprocessing.Process(target=proc_a) | |
p.start() | |
# Wait for thread to finish | |
t.join() | |
# wait for Process to finish. **Hangs** | |
print("waiting for Process to finish...") | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment