|
8 | 8 | ''' |
9 | 9 |
|
10 | 10 | import functools |
| 11 | +import marshal |
11 | 12 |
|
12 | 13 | from python_toolbox import decorator_tools |
13 | 14 |
|
14 | 15 | from . import base_profile |
15 | 16 |
|
16 | 17 |
|
17 | | -def profile_ready(condition=None, off_after=True, sort=2): |
| 18 | +def profile(statement, globals_, locals_): |
| 19 | + profile_ = base_profile.Profile() |
| 20 | + result = None |
| 21 | + try: |
| 22 | + profile_ = profile_.runctx(statement, globals_, locals_) |
| 23 | + except SystemExit: |
| 24 | + pass |
| 25 | + profile_.create_stats() |
| 26 | + profile_result = marshal.dumps(self.stats, f) |
| 27 | + return profile_result |
| 28 | + |
| 29 | + |
| 30 | +def profile_expression(expression, globals_, locals_): |
| 31 | + profile_result = profile('result = %s' % expression, globals(), locals()) |
| 32 | + return (locals()['result'], profile_result) |
| 33 | + |
| 34 | + |
| 35 | +def profile_ready(condition=None, off_after=True, profile_handler=None): |
18 | 36 | ''' |
| 37 | + blocktododoc |
19 | 38 | Decorator for setting a function to be ready for profiling. |
20 | 39 | |
21 | 40 | For example: |
@@ -73,12 +92,14 @@ def inner(function_, *args, **kwargs): |
73 | 92 | # This line puts it in locals, weird: |
74 | 93 | decorated_function.original_function |
75 | 94 |
|
76 | | - base_profile.runctx( |
77 | | - 'result = ' |
| 95 | + result, profile_result = profile_expression( |
78 | 96 | 'decorated_function.original_function(*args, **kwargs)', |
79 | | - globals(), locals(), sort=decorated_function.sort |
80 | | - ) |
81 | | - return locals()['result'] |
| 97 | + globals(), locals() |
| 98 | + ) |
| 99 | + |
| 100 | + Z Z Z Do shit depending on `profile_handler`. Allow filename, folder, email address, or index for printing (sort). In any case do everything on a thread. |
| 101 | + |
| 102 | + return result |
82 | 103 |
|
83 | 104 | else: # decorated_function.profiling_on is False |
84 | 105 |
|
|
0 commit comments