1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
2 | # contributor license agreements. See the NOTICE file distributed with |
||
3 | # this work for additional information regarding copyright ownership. |
||
4 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
||
5 | # (the "License"); you may not use this file except in compliance with |
||
6 | # the License. You may obtain a copy of the License at |
||
7 | # |
||
8 | # http://www.apache.org/licenses/LICENSE-2.0 |
||
9 | # |
||
10 | # Unless required by applicable law or agreed to in writing, software |
||
11 | # distributed under the License is distributed on an "AS IS" BASIS, |
||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
13 | # See the License for the specific language governing permissions and |
||
14 | # |
||
15 | |||
16 | """ |
||
17 | Module for performing eventlet and other monkey patching. |
||
18 | """ |
||
19 | |||
20 | from __future__ import absolute_import |
||
21 | |||
22 | import sys |
||
23 | |||
24 | __all__ = [ |
||
25 | 'monkey_patch', |
||
26 | 'use_select_poll_workaround', |
||
27 | 'is_use_debugger_flag_provided' |
||
28 | ] |
||
29 | |||
30 | USE_DEBUGGER_FLAG = '--use-debugger' |
||
31 | PARENT_ARGS_FLAG = '--parent-args=' |
||
32 | |||
33 | |||
34 | def monkey_patch(): |
||
35 | """ |
||
36 | Function which performs eventlet monkey patching and also takes into account "--use-debugger" |
||
37 | argument in the command line arguments. |
||
38 | |||
39 | If this argument is found, no monkey patching is performed for the thread module. This allows |
||
40 | user to use remote debuggers. |
||
41 | """ |
||
42 | import eventlet |
||
43 | |||
44 | patch_thread = not is_use_debugger_flag_provided() |
||
45 | eventlet.monkey_patch(os=True, select=True, socket=True, thread=patch_thread, time=True) |
||
46 | |||
47 | |||
48 | def use_select_poll_workaround(nose_only=True): |
||
49 | """ |
||
50 | Work around for eventlet monkey patched code which relies on select.poll() functionality |
||
51 | which is blocking and not green / async. |
||
52 | |||
53 | Keep in mind that this was always the case in old eventlet versions, but v0.20.0 removed |
||
54 | select.poll() to avoid users shooting themselves in the foot and rely on blocking code. |
||
55 | |||
56 | This code inserts original blocking select.poll() into the sys.modules so it can be used |
||
57 | where needed. |
||
58 | |||
59 | Keep in mind that this code should only be uses in scenarios where blocking is not a problem. |
||
60 | |||
61 | For example: |
||
62 | |||
63 | 1) Inside tests which rely on subprocess.poll() / select.poll(). |
||
64 | 2) Inside code which relies on 3rd party libraries which use select.poll() and are ran |
||
65 | inside a subprocess so blocking is not an issue (e.g. actions, sensors). |
||
66 | |||
67 | :param nose_only: True to only perform monkey patch when running tests under nose tests |
||
68 | runner. |
||
69 | :type nose_only: ``bool`` |
||
70 | """ |
||
71 | import sys |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
72 | import select |
||
73 | import subprocess |
||
74 | import eventlet |
||
75 | |||
76 | # Work around to get tests to pass with eventlet >= 0.20.0 |
||
77 | if not nose_only or (nose_only and'nose' in sys.modules.keys()): |
||
78 | # Add back blocking poll() to eventlet monkeypatched select |
||
79 | original_poll = eventlet.patcher.original('select').poll |
||
80 | select.poll = original_poll |
||
81 | |||
82 | sys.modules['select'] = select |
||
83 | subprocess.select = select |
||
84 | |||
85 | if sys.version_info >= (3, 6, 5): |
||
86 | # If we also don't patch selectors.select, it will fail with Python >= 3.6.5 |
||
87 | import selectors # pylint: disable=import-error |
||
88 | |||
89 | sys.modules['selectors'] = selectors |
||
90 | selectors.select = sys.modules['select'] |
||
91 | |||
92 | |||
93 | def is_use_debugger_flag_provided(): |
||
94 | # 1. Check sys.argv directly |
||
95 | if USE_DEBUGGER_FLAG in sys.argv: |
||
96 | return True |
||
97 | |||
98 | # 2. Check "parent-args" arguments. This is used for spawned processes such as sensors and |
||
99 | # Python runner actions |
||
100 | |||
101 | for arg in sys.argv: |
||
102 | if arg.startswith(PARENT_ARGS_FLAG) and USE_DEBUGGER_FLAG in arg: |
||
103 | return True |
||
104 | |||
105 | return False |
||
106 |