Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
TST add test for reused semtracker + CLN unused code
  • Loading branch information
tomMoral committed Mar 15, 2019
commit 87964fc80cfaf13fc82057c4a6ef24fbbefefec5
2 changes: 2 additions & 0 deletions Lib/multiprocessing/semaphore_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def ensure_running(self):
def _check_alive(self):
'''Check for that the pipe has not been closed by sending a probe.'''
Comment thread
tomMoral marked this conversation as resolved.
Outdated
try:
# We cannot use send here as it calls ensure_running, creating
# a cycle.
os.write(self._fd, b'PROBE:0\n')
except BrokenPipeError:
Comment thread
tomMoral marked this conversation as resolved.
Outdated
return False
Expand Down
10 changes: 0 additions & 10 deletions Lib/multiprocessing/spawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ def get_preparation_data(name):
start_method=get_start_method(),
)

if sys.platform != "win32":
# Pass the semaphore_tracker pid to avoid re-spawning it in every child
from . import semaphore_tracker
semaphore_tracker.ensure_running()
d['tracker_pid'] = semaphore_tracker._semaphore_tracker._pid

# Figure out whether to initialise main in the subprocess as a module
# or through direct execution (or to leave it alone entirely)
main_module = sys.modules['__main__']
Expand Down Expand Up @@ -237,10 +231,6 @@ def prepare(data):
if 'start_method' in data:
set_start_method(data['start_method'], force=True)

if 'tracker_pid' in data:
from . import semaphore_tracker
semaphore_tracker._semaphore_tracker._pid = data["tracker_pid"]

if 'init_main_from_name' in data:
_fixup_main_from_name(data['init_main_from_name'])
elif 'init_main_from_path' in data:
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4895,6 +4895,33 @@ def test_semaphore_tracker_sigkill(self):
# Uncatchable signal.
self.check_semaphore_tracker_death(signal.SIGKILL, True)

@staticmethod
def _is_semaphore_tracker_reused(conn):
from multiprocessing.semaphore_tracker import _semaphore_tracker
_semaphore_tracker.ensure_running()
reused = _semaphore_tracker._pid is None
reused &= _semaphore_tracker._check_alive()
conn.send(reused)

def test_semaphore_tracker_reused(self):
from multiprocessing.semaphore_tracker import _semaphore_tracker
_semaphore_tracker.ensure_running()
pid = _semaphore_tracker._pid

ctx = multiprocessing.get_context("spawn")
r, w = ctx.Pipe(duplex=False)
p = ctx.Process(target=self._is_semaphore_tracker_reused,
args=(w,))
p.start()
is_semaphore_tracker_reused = r.recv()

# Clean up
p.join()
w.close()
r.close()

self.assertTrue(is_semaphore_tracker_reused)


class TestSimpleQueue(unittest.TestCase):

Expand Down