Debian Codegraph @

pypy/module/thread/test/support.py

raw symbols SCIP
1 import gc
2 import time
3 import thread
4 import os
5 import errno
6
7 from pypy.interpreter.gateway import interp2app, unwrap_spec
8 from rpython.rlib import rgil
9
10
11 NORMAL_TIMEOUT = 300.0 # 5 minutes
12
13
14 def waitfor(space, w_condition, delay=1):
15 adaptivedelay = 0.04
16 limit = time.time() + delay * NORMAL_TIMEOUT
17 while time.time() <= limit:
18 rgil.release()
19 time.sleep(adaptivedelay)
20 rgil.acquire()
21 gc.collect()
22 if space.is_true(space.call_function(w_condition)):
23 return
24 adaptivedelay *= 1.05
25 print '*** timed out ***'
26
27
28 def timeout_killer(cls, pid, delay):
29 def kill():
30 for x in range(delay * 10):
31 time.sleep(0.1)
32 try:
33 os.kill(pid, 0)
34 except OSError as e:
35 if e.errno == errno.ESRCH: # no such process
36 return
37 raise
38 os.kill(pid, 9)
39 print("process %s killed!" % (pid,))
40 import threading
41 threading.Thread(target=kill).start()
42
43
44 class GenericTestThread:
45 spaceconfig = dict(usemodules=('thread', 'time', 'signal'))
46
47 def setup_class(cls):
48 cls.w_runappdirect = cls.space.wrap(cls.runappdirect)
49 if cls.runappdirect:
50 cls.w_NORMAL_TIMEOUT = NORMAL_TIMEOUT
51 def plain_waitfor(cls, condition, delay=1):
52 import gc
53 import time
54 adaptivedelay = 0.04
55 limit = time.time() + cls.NORMAL_TIMEOUT * delay
56 while time.time() <= limit:
57 time.sleep(adaptivedelay)
58 gc.collect()
59 if condition():
60 return
61 adaptivedelay *= 1.05
62 print('*** timed out ***')
63 cls.w_waitfor = plain_waitfor
64
65 cls.w_timeout_killer = timeout_killer
66 else:
67 @unwrap_spec(delay=int)
68 def py_waitfor(space, w_condition, delay=1):
69 waitfor(space, w_condition, delay)
70 cls.w_waitfor = cls.space.wrap(interp2app(py_waitfor))
71
72 def py_timeout_killer(space, __args__):
73 args_w, kwargs_w = __args__.unpack()
74 args = map(space.unwrap, args_w)
75 kwargs = dict([
76 (k, space.unwrap(v))
77 for k, v in kwargs_w.iteritems()
78 ])
79 timeout_killer(cls, *args, **kwargs)
80 cls.w_timeout_killer = cls.space.wrap(interp2app(py_timeout_killer))
81
82 cls.w_busywait = cls.space.appexec([], """():
83 import time
84 return time.sleep
85 """)