Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
fail_switch_three_greenlets.py45 linesDownload Raw Back to tests
1"""
2Uses a trace function to switch greenlets at unexpected times.
3
4In the trace function, we switch from the current greenlet to another
5greenlet, which switches
6"""
7import greenlet
8
9g1 = None
10g2 = None
11
12switch_to_g2 = False
13
14def tracefunc(*args):
15    print('TRACE', *args)
16    global switch_to_g2
17    if switch_to_g2:
18        switch_to_g2 = False
19        g2.switch()
20    print('\tLEAVE TRACE', *args)
21
22def g1_run():
23    print('In g1_run')
24    global switch_to_g2
25    switch_to_g2 = True
26    from_parent = greenlet.getcurrent().parent.switch()
27    print('Return to g1_run')
28    print('From parent', from_parent)
29
30def g2_run():
31    #g1.switch()
32    greenlet.getcurrent().parent.switch()
33
34greenlet.settrace(tracefunc)
35
36g1 = greenlet.greenlet(g1_run)
37g2 = greenlet.greenlet(g2_run)
38
39# This switch didn't actually finish!
40# And if it did, it would raise TypeError
41# because g1_run() doesn't take any arguments.
42g1.switch(1)
43print('Back in main')
44g1.switch(2)
45 
codekingpro/portable-devtools · Team Ai