Source

mino-pypy / pypy / module / thread / test / support.py

Alex Gaynor aa12b7a 





Maciej Fijalkows… 66e4dc3 
Armin Rigo c6beae7 

Armin Rigo 758987f 

Alex Gaynor aa12b7a 
Alex Gaynor c657476 
Nicholas Riley d6d030a 
Armin Rigo 758987f 
Armin Rigo c6beae7 
Maciej Fijalkows… 66e4dc3 
Armin Rigo b1a805e 
Maciej Fijalkows… 66e4dc3 
Armin Rigo c6beae7 


Nicholas Riley d6d030a 
Armin Rigo c6beae7 

Alex Gaynor aa12b7a 
Alex Gaynor c657476 
Amaury Forgeot d… bf5ac00 
Amaury Forgeot d… 657d5ff 


Amaury Forgeot d… bf5ac00 


Armin Rigo 846d29f 
Alex Gaynor aa12b7a 
Armin Rigo 846d29f 
Amaury Forgeot d… 3eb05cb 
Armin Rigo 846d29f 

Amaury Forgeot d… a03792c 
Alex Gaynor e123d4e 
Samuele Pedroni 1df2127 
Armin Rigo 758987f 
Samuele Pedroni 1df2127 






Alex Gaynor aa12b7a 
Samuele Pedroni 1df2127 

Alex Gaynor aa12b7a 




Amaury Forgeot d… 3eb05cb 
Armin Rigo 846d29f 
Armin Rigo c6beae7 
Armin Rigo 846d29f 
Alex Gaynor aa12b7a 









import gc
import time
import thread
import os

from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.module.thread import gil


NORMAL_TIMEOUT = 300.0   # 5 minutes


def waitfor(space, w_condition, delay=1):
    adaptivedelay = 0.04
    limit = time.time() + delay * NORMAL_TIMEOUT
    while time.time() <= limit:
        gil.before_external_call()
        time.sleep(adaptivedelay)
        gil.after_external_call()
        gc.collect()
        if space.is_true(space.call_function(w_condition)):
            return
        adaptivedelay *= 1.05
    print '*** timed out ***'


def timeout_killer(pid, delay):
    def kill():
        for x in range(delay * 10):
            time.sleep(0.1)
            os.kill(pid, 0)
        os.kill(pid, 9)
        print "process %s killed!" % (pid,)
    thread.start_new_thread(kill, ())


class GenericTestThread:
    spaceconfig = dict(usemodules=('thread', 'time', 'signal'))

    def setup_class(cls):
        if cls.runappdirect:
            def plain_waitfor(self, condition, delay=1):
                adaptivedelay = 0.04
                limit = time.time() + NORMAL_TIMEOUT * delay
                while time.time() <= limit:
                    time.sleep(adaptivedelay)
                    gc.collect()
                    if condition():
                        return
                    adaptivedelay *= 1.05
                print '*** timed out ***'

            cls.w_waitfor = plain_waitfor
        else:
            @unwrap_spec(delay=int)
            def py_waitfor(space, w_condition, delay=1):
                waitfor(space, w_condition, delay)

            cls.w_waitfor = cls.space.wrap(interp2app(py_waitfor))
        cls.w_busywait = cls.space.appexec([], """():
            import time
            return time.sleep
        """)

        def py_timeout_killer(space, __args__):
            args_w, kwargs_w = __args__.unpack()
            args = map(space.unwrap, args_w)
            kwargs = dict([
                (k, space.unwrap(v))
                for k, v in kwargs_w.iteritems()
            ])
            timeout_killer(*args, **kwargs)

        cls.w_timeout_killer = cls.space.wrap(interp2app(py_timeout_killer))