aboutsummaryrefslogtreecommitdiffstats
path: root/src/py/imc/nonblock.py
blob: 267bbaad2193b92515b2517f2c35c417df2e1633 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import types

gen_current_id = None
gen_waitmap = {}

def call(f):
    def wrapper(*args,**kwargs):
        global gen_current_id
        global gen_waitmap

        kwargs['_genid'] = gen_current_id
        return f(*args,**kwargs)

    return wrapper

def func(f):
    def wrapper(*args,**kwargs):
        global gen_current_id
        global gen_waitmap

        try:
            gen = f(*args,**kwargs)
            if isinstance(gen,types.GeneratorType):
                gen_current_id = str(id(gen))
                gen_waitmap[gen_current_id] = gen

                try:
                    next(gen)

                    return (False,gen_current_id)

                except StopIteration as ret:
                    del gen_waitmap[gen_current_id]
                    return (True,ret)


            else:
                return (True,gen)

        except Exception:
            return (True,'Einternal')

    return wrapper

def retcall(genid,value):
    global gen_current_id
    global gen_waitmap

    gen_current_id = genid
    try:
        gen = gen_waitmap[gen_current_id]
        gen.send(value)
    
        return (False,gen_current_id)

    except StopIteration as err:
        del gen_waitmap[gen_current_id]
        return (True,err.value)

    except Exception:
        return (True,'Einternal')