Debian Codegraph @

t/unit/test_spawn.py

raw
1 import os
2 import sys
3 from billiard import get_context, Process, Queue
4 from billiard.util import set_pdeathsig, get_pdeathsig
5 import pytest
6 import psutil
7 import signal
8 from time import sleep
9
10 class test_spawn:
11 def test_start(self):
12 ctx = get_context('spawn')
13
14 p = ctx.Process(target=task_from_process, args=('opa',))
15 p.start()
16 p.join()
17 assert p.exitcode == 0
18
19 @pytest.mark.skipif(not sys.platform.startswith('linux'),
20 reason='set_pdeathsig() is supported only in Linux')
21 def test_set_pdeathsig(self):
22 success = "done"
23 q = Queue()
24 p = Process(target=parent_task, args=(q, success))
25 p.start()
26 child_proc = psutil.Process(q.get(timeout=3))
27 try:
28 p.terminate()
29 assert q.get(timeout=3) == success
30 finally:
31 child_proc.terminate()
32
33 @pytest.mark.skipif(not sys.platform.startswith('linux'),
34 reason='get_pdeathsig() is supported only in Linux')
35 def test_set_get_pdeathsig(self):
36 sig = get_pdeathsig()
37 assert sig == 0
38 set_pdeathsig(signal.SIGTERM)
39 sig = get_pdeathsig()
40 assert sig == signal.SIGTERM
41
42 def child_process(q, success):
43 sig = signal.SIGUSR1
44 class ParentDeathError(Exception):
45 pass
46
47 def handler(*args):
48 raise ParentDeathError()
49
50 signal.signal(sig, handler)
51 set_pdeathsig(sig)
52 q.put(os.getpid())
53 try:
54 while True:
55 sleep(1)
56 except ParentDeathError:
57 q.put(success)
58 sys.exit(0)
59
60 def parent_task(q, success):
61 p = Process(target=child_process, args=(q, success))
62 p.start()
63 p.join()
64
65 def task_from_process(name):
66 print('proc:', name)
67