xasyncio 0.2.0__tar.gz → 0.2.2__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.0
3
+ Version: 0.2.2
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
@@ -95,20 +95,103 @@ class AsyncThread(threading.Thread):
95
95
  # if threading.current_thread() != self.thread:
96
96
  # raise ThreadingError('Invalid thread: this function must be called in the loop thread')
97
97
 
98
- def await_coroutine(self, coro):
99
- # task = self.loop.create_task(coro)
100
- finish_event = threading.Event()
98
+ def await_coroutine(self, coro, timeout=None):
99
+ return asyncio.run_coroutine_threadsafe(coro, self.loop).result(timeout)
100
+
101
+ # finish_event = threading.Event()
102
+ #
103
+ # async def _helper():
104
+ # try:
105
+ # await coro
106
+ # except Exception as e:
107
+ # traceback.print_exc()
108
+ #
109
+ # finish_event.set()
110
+ #
111
+ # self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
112
+ # finish_event.wait()
113
+
114
+
115
+ class AsyncedThread:
116
+ def __init__(self, name, thread):
117
+ self.thread = thread
118
+ self.loop = asyncio.get_event_loop()
119
+ assert self.loop.is_running() # we require the loop already running
120
+ self.name = name
121
+ self.events = {}
122
+ self.events_out_thread = {}
123
+ self.stopped = True
101
124
 
102
- async def _helper():
103
- try:
104
- await coro
105
- except Exception as e:
106
- traceback.print_exc()
125
+ def _stop(self):
126
+ """this function must be called with the thread"""
127
+ if self.stopped:
128
+ return
129
+ self.loop.stop()
130
+ self.stopped = True
107
131
 
108
- finish_event.set()
132
+ def stop(self):
133
+ """thread safe stop function"""
134
+ # thread = threading.current_thread()
135
+ # if thread == self.thread:
136
+ # print('calling from the loop thread')
137
+ # else:
138
+ # print('calling from another loop')
139
+ print(f'Threaded loop {self.name} stopping')
140
+ self.call_sync(self._stop)
141
+ # self.thread.join(10)
142
+ self.join(10)
143
+
144
+ def _mark_running(self, running=True):
145
+ # if running:
146
+ # print('mark running')
147
+ # else:
148
+ # print('mark stopped')
149
+ self.stopped = not running
150
+
151
+ def create_out_thread_event(self, name):
152
+ self.events_out_thread[name] = threading.Event()
153
+
154
+ def wait_out_thread_event(self, name):
155
+ self.events_out_thread[name].wait()
156
+
157
+ def notify_out_thread_event(self, name):
158
+ # print('notify event', name)
159
+ self.events_out_thread[name].set()
160
+
161
+ def create_event(self, name):
162
+ self.events[name] = threading.Event()
163
+
164
+ def notify(self, event_name):
165
+ self.events[event_name].set()
166
+
167
+ def wait(self, event_name):
168
+ self.events[event_name].wait()
169
+
170
+ def call_sync(self, func, *args):
171
+ blocking_call_w_loop(self.loop, func, *args)
172
+
173
+ def call_async(self, func, *args):
174
+ self.loop.call_soon_threadsafe(func, *args)
175
+
176
+ # def _sync_coro(self, coro):
177
+ # if threading.current_thread() != self.thread:
178
+ # raise ThreadingError('Invalid thread: this function must be called in the loop thread')
109
179
 
110
- self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
111
- finish_event.wait()
180
+ def await_coroutine(self, coro, timeout=None):
181
+ return asyncio.run_coroutine_threadsafe(coro, self.loop).result(timeout)
182
+
183
+ # finish_event = threading.Event()
184
+ #
185
+ # async def _helper():
186
+ # try:
187
+ # await coro
188
+ # except Exception as e:
189
+ # traceback.print_exc()
190
+ #
191
+ # finish_event.set()
192
+ #
193
+ # self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
194
+ # finish_event.wait()
112
195
 
113
196
 
114
197
  def blocking_call_w_loop(loop, func, *args):
File without changes
File without changes
File without changes