xasyncio 0.1__py3-none-any.whl → 0.2.0__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
@@ -1,16 +1,23 @@
1
1
  import asyncio
2
2
  import threading
3
+ import traceback
3
4
 
5
+ from typing import *
4
6
 
5
- class ThreadedEventLoop:
7
+
8
+ class ThreadingError(Exception):
9
+ pass
10
+
11
+
12
+ class AsyncThread(threading.Thread):
6
13
  def __init__(self, name):
14
+ super().__init__()
7
15
  self.name = name
8
- self.thread = threading.Thread(target=self.run)
9
16
  self.events = {}
10
17
  self.events_out_thread = {}
11
- self.loop = None
18
+ self.loop: asyncio.BaseEventLoop | None = None
12
19
  self.stopped = True
13
- self.thread.start()
20
+ self.start()
14
21
  self.create_out_thread_event('loop_started')
15
22
  self.wait_out_thread_event('loop_started')
16
23
 
@@ -28,8 +35,10 @@ class ThreadedEventLoop:
28
35
  # print('calling from the loop thread')
29
36
  # else:
30
37
  # print('calling from another loop')
38
+ print(f'Threaded loop {self.name} stopping')
31
39
  self.call_sync(self._stop)
32
- self.thread.join(10)
40
+ # self.thread.join(10)
41
+ self.join(10)
33
42
 
34
43
  def _mark_running(self, running=True):
35
44
  # if running:
@@ -40,11 +49,14 @@ class ThreadedEventLoop:
40
49
 
41
50
  def run(self):
42
51
  self.loop = asyncio.new_event_loop()
43
- # self.loop = asyncio.get_event_loop()
44
52
  # Need to call this in the loop, mainly because need to make sure the loop is running
53
+ # debugging version
45
54
  # self.loop.call_soon_threadsafe(
46
55
  # lambda: (
47
- # 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')))
48
60
  self.loop.call_soon_threadsafe(
49
61
  lambda: (
50
62
  self._mark_running(), self.notify_out_thread_event('loop_started')
@@ -61,7 +73,7 @@ class ThreadedEventLoop:
61
73
  self.events_out_thread[name].wait()
62
74
 
63
75
  def notify_out_thread_event(self, name):
64
- print('notify event', name)
76
+ # print('notify event', name)
65
77
  self.events_out_thread[name].set()
66
78
 
67
79
  def create_event(self, name):
@@ -79,13 +91,35 @@ class ThreadedEventLoop:
79
91
  def call_async(self, func, *args):
80
92
  self.loop.call_soon_threadsafe(func, *args)
81
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
+
82
113
 
83
114
  def blocking_call_w_loop(loop, func, *args):
84
115
  finish_event = threading.Event()
85
116
 
86
117
  def _helper():
87
- func(*args)
88
- finish_event.set()
118
+ try:
119
+ func(*args)
120
+ finish_event.set()
121
+ except Exception:
122
+ traceback.print_exc()
89
123
 
90
124
  loop.call_soon_threadsafe(_helper)
91
125
  finish_event.wait()
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xasyncio
3
- Version: 0.1
4
- Summary: A package to publish and receive messages
3
+ Version: 0.2.0
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
7
7
  Author-email: Yisu Peng <yisupeng@gmail.com>
@@ -0,0 +1,5 @@
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,,
@@ -1,5 +0,0 @@
1
- xasyncio/__init__.py,sha256=mewp9FqqRAvpq0R7JvUisQha3s-DXN0OXlvrlw0CRK8,3178
2
- xasyncio-0.1.dist-info/METADATA,sha256=NlNuwSMNgnhritpq3F3HBNSZVAzaMYk5JSWnjIxKF5U,471
3
- xasyncio-0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
- xasyncio-0.1.dist-info/licenses/LICENSE,sha256=fHWKRzBMuHQa8EYpadXv8pSmA8XnR8FlirkvLoayV1I,1066
5
- xasyncio-0.1.dist-info/RECORD,,