xasyncio 0.2.0__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of xasyncio might be problematic. Click here for more details.
- {xasyncio-0.2.0 → xasyncio-0.2.1}/PKG-INFO +1 -1
- {xasyncio-0.2.0 → xasyncio-0.2.1}/src/xasyncio/__init__.py +81 -0
- {xasyncio-0.2.0 → xasyncio-0.2.1}/.github/workflows/release.yml +0 -0
- {xasyncio-0.2.0 → xasyncio-0.2.1}/.gitignore +0 -0
- {xasyncio-0.2.0 → xasyncio-0.2.1}/LICENSE +0 -0
- {xasyncio-0.2.0 → xasyncio-0.2.1}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: xasyncio
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A package to simiplify multithreaded asyncio event loops
|
|
5
5
|
Project-URL: Homepage, https://github.com/shawn-peng/xasyncio
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/shawn-peng/xasyncio/issues
|
|
@@ -111,6 +111,87 @@ class AsyncThread(threading.Thread):
|
|
|
111
111
|
finish_event.wait()
|
|
112
112
|
|
|
113
113
|
|
|
114
|
+
class AsyncedThread:
|
|
115
|
+
def __init__(self, name, thread):
|
|
116
|
+
self.thread = thread
|
|
117
|
+
self.loop = asyncio.get_event_loop()
|
|
118
|
+
assert self.loop.is_running() # we require the loop already running
|
|
119
|
+
self.name = name
|
|
120
|
+
self.events = {}
|
|
121
|
+
self.events_out_thread = {}
|
|
122
|
+
self.stopped = True
|
|
123
|
+
|
|
124
|
+
def _stop(self):
|
|
125
|
+
"""this function must be called with the thread"""
|
|
126
|
+
if self.stopped:
|
|
127
|
+
return
|
|
128
|
+
self.loop.stop()
|
|
129
|
+
self.stopped = True
|
|
130
|
+
|
|
131
|
+
def stop(self):
|
|
132
|
+
"""thread safe stop function"""
|
|
133
|
+
# thread = threading.current_thread()
|
|
134
|
+
# if thread == self.thread:
|
|
135
|
+
# print('calling from the loop thread')
|
|
136
|
+
# else:
|
|
137
|
+
# print('calling from another loop')
|
|
138
|
+
print(f'Threaded loop {self.name} stopping')
|
|
139
|
+
self.call_sync(self._stop)
|
|
140
|
+
# self.thread.join(10)
|
|
141
|
+
self.join(10)
|
|
142
|
+
|
|
143
|
+
def _mark_running(self, running=True):
|
|
144
|
+
# if running:
|
|
145
|
+
# print('mark running')
|
|
146
|
+
# else:
|
|
147
|
+
# print('mark stopped')
|
|
148
|
+
self.stopped = not running
|
|
149
|
+
|
|
150
|
+
def create_out_thread_event(self, name):
|
|
151
|
+
self.events_out_thread[name] = threading.Event()
|
|
152
|
+
|
|
153
|
+
def wait_out_thread_event(self, name):
|
|
154
|
+
self.events_out_thread[name].wait()
|
|
155
|
+
|
|
156
|
+
def notify_out_thread_event(self, name):
|
|
157
|
+
# print('notify event', name)
|
|
158
|
+
self.events_out_thread[name].set()
|
|
159
|
+
|
|
160
|
+
def create_event(self, name):
|
|
161
|
+
self.events[name] = threading.Event()
|
|
162
|
+
|
|
163
|
+
def notify(self, event_name):
|
|
164
|
+
self.events[event_name].set()
|
|
165
|
+
|
|
166
|
+
def wait(self, event_name):
|
|
167
|
+
self.events[event_name].wait()
|
|
168
|
+
|
|
169
|
+
def call_sync(self, func, *args):
|
|
170
|
+
blocking_call_w_loop(self.loop, func, *args)
|
|
171
|
+
|
|
172
|
+
def call_async(self, func, *args):
|
|
173
|
+
self.loop.call_soon_threadsafe(func, *args)
|
|
174
|
+
|
|
175
|
+
# def _sync_coro(self, coro):
|
|
176
|
+
# if threading.current_thread() != self.thread:
|
|
177
|
+
# raise ThreadingError('Invalid thread: this function must be called in the loop thread')
|
|
178
|
+
|
|
179
|
+
def await_coroutine(self, coro):
|
|
180
|
+
# task = self.loop.create_task(coro)
|
|
181
|
+
finish_event = threading.Event()
|
|
182
|
+
|
|
183
|
+
async def _helper():
|
|
184
|
+
try:
|
|
185
|
+
await coro
|
|
186
|
+
except Exception as e:
|
|
187
|
+
traceback.print_exc()
|
|
188
|
+
|
|
189
|
+
finish_event.set()
|
|
190
|
+
|
|
191
|
+
self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
|
|
192
|
+
finish_event.wait()
|
|
193
|
+
|
|
194
|
+
|
|
114
195
|
def blocking_call_w_loop(loop, func, *args):
|
|
115
196
|
finish_event = threading.Event()
|
|
116
197
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|