xasyncio 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl

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/__init__.py CHANGED
@@ -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
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xasyncio
3
- Version: 0.2.0
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
@@ -0,0 +1,5 @@
1
+ xasyncio/__init__.py,sha256=rs9oGlXapDlLBTVojyGzJPnJlMGYwiJRf59ryNK2tpE,6480
2
+ xasyncio-0.2.1.dist-info/METADATA,sha256=nEu4aqMmsegNBBDqoo7P4VWnnYXexLRA4fcieOPnoQY,488
3
+ xasyncio-0.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
+ xasyncio-0.2.1.dist-info/licenses/LICENSE,sha256=fHWKRzBMuHQa8EYpadXv8pSmA8XnR8FlirkvLoayV1I,1066
5
+ xasyncio-0.2.1.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- xasyncio/__init__.py,sha256=IzdT5rpma3Rs0rh-Seqtg-JrNWTJaVEB9ULwU3-DX74,4060
2
- xasyncio-0.2.0.dist-info/METADATA,sha256=WhgRH2CYfUAWmaHnlsRyNBbDzyiY1rkn_FO9hXVEC0U,488
3
- xasyncio-0.2.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
- xasyncio-0.2.0.dist-info/licenses/LICENSE,sha256=fHWKRzBMuHQa8EYpadXv8pSmA8XnR8FlirkvLoayV1I,1066
5
- xasyncio-0.2.0.dist-info/RECORD,,