Skip to content

Instantly share code, notes, and snippets.

@mfm24
Created November 15, 2019 19:44
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 mfm24/e62ec5d50c672524107ca00a391e6104 to your computer and use it in GitHub Desktop.
Save mfm24/e62ec5d50c672524107ca00a391e6104 to your computer and use it in GitHub Desktop.
Multiprocessing Logging Deadlock
# 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