xasyncio 0.1.7__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xasyncio
3
- Version: 0.1.7
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
@@ -2,20 +2,22 @@ import asyncio
2
2
  import threading
3
3
  import traceback
4
4
 
5
+ from typing import *
6
+
5
7
 
6
8
  class ThreadingError(Exception):
7
9
  pass
8
10
 
9
11
 
10
- class ThreadedEventLoop:
12
+ class AsyncThread(threading.Thread):
11
13
  def __init__(self, name):
14
+ super().__init__()
12
15
  self.name = name
13
- self.thread = threading.Thread(target=self.run)
14
16
  self.events = {}
15
17
  self.events_out_thread = {}
16
- self.loop: asyncio.BaseEventLoop = None
18
+ self.loop: asyncio.BaseEventLoop | None = None
17
19
  self.stopped = True
18
- self.thread.start()
20
+ self.start()
19
21
  self.create_out_thread_event('loop_started')
20
22
  self.wait_out_thread_event('loop_started')
21
23
 
@@ -35,7 +37,8 @@ class ThreadedEventLoop:
35
37
  # print('calling from another loop')
36
38
  print(f'Threaded loop {self.name} stopping')
37
39
  self.call_sync(self._stop)
38
- self.thread.join(10)
40
+ # self.thread.join(10)
41
+ self.join(10)
39
42
 
40
43
  def _mark_running(self, running=True):
41
44
  # if running:
@@ -46,11 +49,14 @@ class ThreadedEventLoop:
46
49
 
47
50
  def run(self):
48
51
  self.loop = asyncio.new_event_loop()
49
- # self.loop = asyncio.get_event_loop()
50
52
  # Need to call this in the loop, mainly because need to make sure the loop is running
53
+ # debugging version
51
54
  # self.loop.call_soon_threadsafe(
52
55
  # lambda: (
53
- # print('notifying loop started'), self._mark_running(), print(self.stopped), self.notify_out_thread_event('loop_started')))
56
+ # print('notifying loop started'),
57
+ # self._mark_running(),
58
+ # print(self.stopped),
59
+ # self.notify_out_thread_event('loop_started')))
54
60
  self.loop.call_soon_threadsafe(
55
61
  lambda: (
56
62
  self._mark_running(), self.notify_out_thread_event('loop_started')
@@ -105,6 +111,87 @@ class ThreadedEventLoop:
105
111
  finish_event.wait()
106
112
 
107
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
+
108
195
  def blocking_call_w_loop(loop, func, *args):
109
196
  finish_event = threading.Event()
110
197
 
File without changes
File without changes
File without changes