xasyncio 0.2.1__tar.gz → 0.2.3__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xasyncio
3
- Version: 0.2.1
3
+ Version: 0.2.3
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
@@ -0,0 +1,249 @@
1
+ import asyncio
2
+ import dataclasses
3
+ import logging
4
+ import threading
5
+ import traceback
6
+
7
+ from typing import *
8
+
9
+
10
+ class ThreadingError(Exception):
11
+ pass
12
+
13
+
14
+ @dataclasses.dataclass
15
+ class AsyncThreadBase:
16
+ # def call_async(self, func, *args):
17
+ # raise NotImplementedError
18
+ #
19
+ # def call_sync(self, func, *args):
20
+ # raise NotImplementedError
21
+ #
22
+ # def sync_coroutine(self, coro, timeout=None):
23
+ # raise NotImplementedError
24
+ #
25
+ # def ensure_coroutine(self, coro):
26
+ # raise NotImplementedError
27
+ #
28
+ # async def await_coroutine(self, coro):
29
+ # raise NotImplementedError
30
+ #
31
+ # def stop(self):
32
+ # raise NotImplementedError
33
+ name: str = ''
34
+ loop: asyncio.AbstractEventLoop | None = None
35
+ events: dict = dataclasses.field(default_factory=dict)
36
+ events_out_thread: dict = dataclasses.field(default_factory=dict)
37
+
38
+ def _stop_self(self):
39
+ """this function must be called with this thread"""
40
+ if self.stopped:
41
+ return
42
+ self.loop.stop()
43
+ self.stopped = True
44
+
45
+ async def stop(self):
46
+ """thread safe stop function"""
47
+ # Called in other thread
48
+
49
+ # thread = threading.current_thread()
50
+ # if thread == self.thread:
51
+ # print('calling from the loop thread')
52
+ # else:
53
+ # print('calling from another loop')
54
+ print(f'Threaded loop {self.name} stopping')
55
+ await self.call_sync(self._stop_self)
56
+
57
+ def _mark_running(self, running=True):
58
+ # if running:
59
+ # print('mark running')
60
+ # else:
61
+ # print('mark stopped')
62
+ self.stopped = not running
63
+
64
+ def create_out_thread_event(self, name):
65
+ self.events_out_thread[name] = threading.Event()
66
+
67
+ def wait_out_thread_event(self, name):
68
+ self.events_out_thread[name].wait()
69
+
70
+ def notify_out_thread_event(self, name):
71
+ # print('notify event', name)
72
+ self.events_out_thread[name].set()
73
+
74
+ def create_event(self, name):
75
+ self.events[name] = threading.Event()
76
+
77
+ def notify(self, event_name):
78
+ self.events[event_name].set()
79
+
80
+ def wait(self, event_name):
81
+ self.events[event_name].wait()
82
+
83
+ # def call_blocking(self, func, *args):
84
+ # def call_sync(self, func, *args):
85
+ # # other thread will be blocked and could result in deadlocks
86
+ # pass
87
+
88
+ async def call_sync(self, func, *args):
89
+ # Called in other thread
90
+ # blocking_call_w_loop(self.loop, func, *args)
91
+ finish_event = asyncio.Event()
92
+ caller_loop = asyncio.get_event_loop()
93
+
94
+ def _set_finish_event():
95
+ caller_loop.call_soon_threadsafe(lambda: finish_event.set())
96
+
97
+ def _helper():
98
+ # in self thread
99
+ try:
100
+ func(*args)
101
+ _set_finish_event()
102
+ except Exception as e:
103
+ nonlocal exception
104
+ traceback.print_exc()
105
+ exception = e
106
+ _set_finish_event()
107
+
108
+ exception = None
109
+
110
+ def _ex_handler(e):
111
+ nonlocal exception
112
+ exception = e
113
+ raise exception
114
+
115
+ self.loop.set_exception_handler(_ex_handler)
116
+
117
+ self.loop.call_soon_threadsafe(_helper)
118
+ await finish_event.wait()
119
+ if exception:
120
+ raise exception
121
+
122
+ def call_async(self, func, *args):
123
+ self.loop.call_soon_threadsafe(func, *args)
124
+
125
+ # def _sync_coro(self, coro):
126
+ # if threading.current_thread() != self.thread:
127
+ # raise ThreadingError('Invalid thread: this function must be called in the loop thread')
128
+
129
+ def sync_coroutine(self, coro, timeout=None):
130
+ return asyncio.run_coroutine_threadsafe(coro, self.loop).result(timeout)
131
+
132
+ def ensure_coroutine(self, coro):
133
+ return asyncio.run_coroutine_threadsafe(coro, self.loop)
134
+
135
+ async def run_coroutine(self, coro):
136
+ return await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coro, self.loop))
137
+
138
+ def blocking_call_w_loop(self, loop, func, *args):
139
+ pass
140
+
141
+ # def sync_coroutine_deadlock(self, coro, timeout=None):
142
+ # finish_event = threading.Event()
143
+ #
144
+ # async def _helper():
145
+ # try:
146
+ # await coro
147
+ # except Exception as e:
148
+ # traceback.print_exc()
149
+ #
150
+ # finish_event.set()
151
+ #
152
+ # self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
153
+ # finish_event.wait()
154
+
155
+
156
+ class AsyncThread(threading.Thread, AsyncThreadBase):
157
+ def __init__(self, name):
158
+ # super(AsyncThread, self).__init__()
159
+ threading.Thread.__init__(self)
160
+ AsyncThreadBase.__init__(self)
161
+ self.name = name
162
+ self.events = {}
163
+ self.events_out_thread = {}
164
+ self.loop: asyncio.BaseEventLoop | None = None
165
+ self.stopped = True
166
+ self.create_out_thread_event('loop_started')
167
+ self.start()
168
+ # self.wait_out_thread_event('loop_started')
169
+ logging.debug('AsyncThread init finished and running')
170
+
171
+ def __repr__(self):
172
+ return f'<AsyncThread {self.name}>'
173
+
174
+ async def stop(self):
175
+ # Called in other thread
176
+ await super().stop()
177
+ res = self.join(1)
178
+ print(res)
179
+
180
+ def run(self):
181
+ self.loop = asyncio.new_event_loop()
182
+ asyncio.set_event_loop(self.loop)
183
+
184
+ # Need to call this in the loop, mainly because need to make sure the loop is running
185
+ # debugging version
186
+ # self.loop.call_soon_threadsafe(
187
+ # lambda: (
188
+ # print('notifying loop started'),
189
+ # self._mark_running(),
190
+ # print(self.stopped),
191
+ # self.notify_out_thread_event('loop_started')))
192
+
193
+ # self.loop.call_soon_threadsafe(
194
+ # self.loop.call_soon(
195
+ # lambda: (
196
+ # self._mark_running(), self.notify_out_thread_event('loop_started')
197
+ # )
198
+ # )
199
+
200
+ async def _notify_running():
201
+ logging.debug('notify running')
202
+ self._mark_running()
203
+ self.notify_out_thread_event('loop_started')
204
+
205
+ asyncio.ensure_future(_notify_running())
206
+
207
+ logging.info('running loop')
208
+ self.loop.run_forever()
209
+ print(f'Loop ({self.name}) finished')
210
+
211
+ def __hash__(self):
212
+ return 0
213
+
214
+ def __eq__(self, other):
215
+ return self is other
216
+
217
+
218
+ class AsyncedThread(AsyncThreadBase):
219
+ def __init__(self, name, thread):
220
+ self.thread = thread
221
+ self.loop = asyncio.get_event_loop()
222
+ assert self.loop.is_running() # we require the loop already running
223
+ self.name = name
224
+ self.events = {}
225
+ self.events_out_thread = {}
226
+ self.stopped = True
227
+
228
+
229
+ class ThreadSafeEvent(asyncio.Event):
230
+ def set(self):
231
+ self._loop.call_soon_threadsafe(super().set)
232
+
233
+ def clear(self):
234
+ self._loop.call_soon_threadsafe(super().clear)
235
+
236
+
237
+ class ThreadSafeSemaphore(asyncio.Semaphore):
238
+ pass
239
+
240
+
241
+ async def cross_thread_call(loop: asyncio.BaseEventLoop, func, *args):
242
+ done = ThreadSafeEvent()
243
+
244
+ def _helper():
245
+ func(*args)
246
+ done.set()
247
+
248
+ loop.call_soon_threadsafe(_helper)
249
+ await done.wait()
@@ -1,229 +0,0 @@
1
- import asyncio
2
- import threading
3
- import traceback
4
-
5
- from typing import *
6
-
7
-
8
- class ThreadingError(Exception):
9
- pass
10
-
11
-
12
- class AsyncThread(threading.Thread):
13
- def __init__(self, name):
14
- super().__init__()
15
- self.name = name
16
- self.events = {}
17
- self.events_out_thread = {}
18
- self.loop: asyncio.BaseEventLoop | None = None
19
- self.stopped = True
20
- self.start()
21
- self.create_out_thread_event('loop_started')
22
- self.wait_out_thread_event('loop_started')
23
-
24
- def _stop(self):
25
- """this function must be called with the thread"""
26
- if self.stopped:
27
- return
28
- self.loop.stop()
29
- self.stopped = True
30
-
31
- def stop(self):
32
- """thread safe stop function"""
33
- # thread = threading.current_thread()
34
- # if thread == self.thread:
35
- # print('calling from the loop thread')
36
- # else:
37
- # print('calling from another loop')
38
- print(f'Threaded loop {self.name} stopping')
39
- self.call_sync(self._stop)
40
- # self.thread.join(10)
41
- self.join(10)
42
-
43
- def _mark_running(self, running=True):
44
- # if running:
45
- # print('mark running')
46
- # else:
47
- # print('mark stopped')
48
- self.stopped = not running
49
-
50
- def run(self):
51
- self.loop = asyncio.new_event_loop()
52
- # Need to call this in the loop, mainly because need to make sure the loop is running
53
- # debugging version
54
- # self.loop.call_soon_threadsafe(
55
- # lambda: (
56
- # print('notifying loop started'),
57
- # self._mark_running(),
58
- # print(self.stopped),
59
- # self.notify_out_thread_event('loop_started')))
60
- self.loop.call_soon_threadsafe(
61
- lambda: (
62
- self._mark_running(), self.notify_out_thread_event('loop_started')
63
- )
64
- )
65
-
66
- self.loop.run_forever()
67
- print(f'Loop ({self.name}) finished')
68
-
69
- def create_out_thread_event(self, name):
70
- self.events_out_thread[name] = threading.Event()
71
-
72
- def wait_out_thread_event(self, name):
73
- self.events_out_thread[name].wait()
74
-
75
- def notify_out_thread_event(self, name):
76
- # print('notify event', name)
77
- self.events_out_thread[name].set()
78
-
79
- def create_event(self, name):
80
- self.events[name] = threading.Event()
81
-
82
- def notify(self, event_name):
83
- self.events[event_name].set()
84
-
85
- def wait(self, event_name):
86
- self.events[event_name].wait()
87
-
88
- def call_sync(self, func, *args):
89
- blocking_call_w_loop(self.loop, func, *args)
90
-
91
- def call_async(self, func, *args):
92
- self.loop.call_soon_threadsafe(func, *args)
93
-
94
- # def _sync_coro(self, coro):
95
- # if threading.current_thread() != self.thread:
96
- # raise ThreadingError('Invalid thread: this function must be called in the loop thread')
97
-
98
- def await_coroutine(self, coro):
99
- # task = self.loop.create_task(coro)
100
- finish_event = threading.Event()
101
-
102
- async def _helper():
103
- try:
104
- await coro
105
- except Exception as e:
106
- traceback.print_exc()
107
-
108
- finish_event.set()
109
-
110
- self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
111
- finish_event.wait()
112
-
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
-
195
- def blocking_call_w_loop(loop, func, *args):
196
- finish_event = threading.Event()
197
-
198
- def _helper():
199
- try:
200
- func(*args)
201
- finish_event.set()
202
- except Exception:
203
- traceback.print_exc()
204
-
205
- loop.call_soon_threadsafe(_helper)
206
- finish_event.wait()
207
-
208
-
209
- class ThreadSafeEvent(asyncio.Event):
210
- def set(self):
211
- self._loop.call_soon_threadsafe(super().set)
212
-
213
- def clear(self):
214
- self._loop.call_soon_threadsafe(super().clear)
215
-
216
-
217
- class ThreadSafeSemaphore(asyncio.Semaphore):
218
- pass
219
-
220
-
221
- async def cross_thread_call(loop: asyncio.BaseEventLoop, func, *args):
222
- done = ThreadSafeEvent()
223
-
224
- def _helper():
225
- func(*args)
226
- done.set()
227
-
228
- loop.call_soon_threadsafe(_helper)
229
- await done.wait()
File without changes
File without changes
File without changes