Issue 29658: Combining thread and process, process hangs (sometimes) (original) (raw)

Issue29658

Created on 2017-02-26 13:48 by djstrong, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg288594 - (view) Author: (djstrong) Date: 2017-02-26 13:48
I have this code, which sometimes hangs (1 of 5 runs): # -*- coding: utf-8 -*- import sys import threading import multiprocessing mpl = multiprocessing.log_to_stderr() mpl.setLevel(1) class A(threading.Thread): def __init__(self): super(A, self).__init__() def run(self): print(self.name, 'RUN') for line in sys.stdin: #increases probability of hanging pass print(self.name, 'STOP') class B(multiprocessing.Process): def __init__(self): super(B, self).__init__() def run(self): print(self.name, 'RUN') a = A() a.start() b = B() b.start() print('B started', b, b.is_alive(), b.pid) print('A', a) a.join() b.join() print('FINISHED') I am running it on Ubuntu with command: echo "" | python3 time_test4.py Output is: Thread-1 RUN B started <B(B-1, started)> True 31439 A <A(Thread-1, started 139779252864768)> Thread-1 STOP
msg288595 - (view) Author: (djstrong) Date: 2017-02-26 13:58
Sometimes the output is: Thread-1 RUN Thread-1 STOP B started <B(B-1, started)> True 20187 A <A(Thread-1, started 139773917562624)> [INFO/B-1] child process calling self.run() but "print" in "run" method of process is not executed.
msg288610 - (view) Author: (djstrong) Date: 2017-02-26 19:17
It is confirmed with other hardware with Python 3.5.2. Process is forked, but method "run" is not executing.
msg288624 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-02-27 09:39
Unless I'm misreading what you're trying to do, this doesn't seem like a bug to me. In general, when combining threading with multiple processes, the rule is to fork first and then launch threads (so that none of the thread ids or locks accidentally get shared across processes).
msg288641 - (view) Author: (djstrong) Date: 2017-02-27 13:39
Thank you! I couldn't find information about first forking then launching threads.
History
Date User Action Args
2022-04-11 14:58:43 admin set github: 73844
2017-02-27 13:39:57 djstrong set status: open -> closedresolution: not a bugmessages: + stage: resolved
2017-02-27 09:39:44 rhettinger set assignee: davinmessages: + nosy: + davin, rhettinger
2017-02-26 19:17:52 djstrong set messages: + versions: + Python 3.5, - Python 3.4
2017-02-26 13:58:23 djstrong set messages: +
2017-02-26 13:49:23 djstrong set title: Process hangs (sometimes) -> Combining thread and process, process hangs (sometimes)
2017-02-26 13:48:30 djstrong create