-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrace.py
61 lines (51 loc) · 1.36 KB
/
trace.py
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
import time
import traceback
import eventlet.corolocal
import eventlet.greenthread
old_main = None
out = None
def patch_eventlet(_out):
'''Patches eventlet so that GreenThread will output trace to a file
when created and run.
'''
if hasattr(eventlet.greenthread.GreenThread, 'vpatched'):
return
eventlet.greenthread.GreenThread.vpatched = True
global out
out = _out
global old_main
old_main = eventlet.greenthread.GreenThread.main
eventlet.greenthread.GreenThread.main = new_main
global old_init
old_init = eventlet.greenthread.GreenThread.__init__
eventlet.greenthread.GreenThread.__init__ = new_init
def new_main(self, *args):
'''Override for the main method for a GreenThread.
'''
global old_main
_id = id(self)
try:
out.write('S\n%s\n%s\n%s\n' % (
_id,
time.time(),
repr(args).replace('\n', '\\n'),
))
return old_main(self, *args)
finally:
out.write('E\n%s\n%s\n' % (
_id,
time.time(),
))
out.flush()
def new_init(self, parent):
'''Overrides for the creation of a GreenThread.
'''
global old_init
_id = id(self)
stack = traceback.format_stack()
out.write('C\n%s\n%s\n%s\n' % (
_id,
time.time(),
stack,
))
old_init(self, parent)