xasyncio 0.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 ADDED
@@ -0,0 +1,114 @@
1
+ import asyncio
2
+ import threading
3
+
4
+
5
+ class ThreadedEventLoop:
6
+ def __init__(self, name):
7
+ self.name = name
8
+ self.thread = threading.Thread(target=self.run)
9
+ self.events = {}
10
+ self.events_out_thread = {}
11
+ self.loop = None
12
+ self.stopped = True
13
+ self.thread.start()
14
+ self.create_out_thread_event('loop_started')
15
+ self.wait_out_thread_event('loop_started')
16
+
17
+ def _stop(self):
18
+ """this function must be called with the thread"""
19
+ if self.stopped:
20
+ return
21
+ self.loop.stop()
22
+ self.stopped = True
23
+
24
+ def stop(self):
25
+ """thread safe stop function"""
26
+ # thread = threading.current_thread()
27
+ # if thread == self.thread:
28
+ # print('calling from the loop thread')
29
+ # else:
30
+ # print('calling from another loop')
31
+ self.call_sync(self._stop)
32
+ self.thread.join(10)
33
+
34
+ def _mark_running(self, running=True):
35
+ # if running:
36
+ # print('mark running')
37
+ # else:
38
+ # print('mark stopped')
39
+ self.stopped = not running
40
+
41
+ def run(self):
42
+ self.loop = asyncio.new_event_loop()
43
+ # self.loop = asyncio.get_event_loop()
44
+ # Need to call this in the loop, mainly because need to make sure the loop is running
45
+ # self.loop.call_soon_threadsafe(
46
+ # lambda: (
47
+ # print('notifying loop started'), self._mark_running(), print(self.stopped), self.notify_out_thread_event('loop_started')))
48
+ self.loop.call_soon_threadsafe(
49
+ lambda: (
50
+ self._mark_running(), self.notify_out_thread_event('loop_started')
51
+ )
52
+ )
53
+
54
+ self.loop.run_forever()
55
+ print(f'Loop ({self.name}) finished')
56
+
57
+ def create_out_thread_event(self, name):
58
+ self.events_out_thread[name] = threading.Event()
59
+
60
+ def wait_out_thread_event(self, name):
61
+ self.events_out_thread[name].wait()
62
+
63
+ def notify_out_thread_event(self, name):
64
+ print('notify event', name)
65
+ self.events_out_thread[name].set()
66
+
67
+ def create_event(self, name):
68
+ self.events[name] = threading.Event()
69
+
70
+ def notify(self, event_name):
71
+ self.events[event_name].set()
72
+
73
+ def wait(self, event_name):
74
+ self.events[event_name].wait()
75
+
76
+ def call_sync(self, func, *args):
77
+ blocking_call_w_loop(self.loop, func, *args)
78
+
79
+ def call_async(self, func, *args):
80
+ self.loop.call_soon_threadsafe(func, *args)
81
+
82
+
83
+ def blocking_call_w_loop(loop, func, *args):
84
+ finish_event = threading.Event()
85
+
86
+ def _helper():
87
+ func(*args)
88
+ finish_event.set()
89
+
90
+ loop.call_soon_threadsafe(_helper)
91
+ finish_event.wait()
92
+
93
+
94
+ class ThreadSafeEvent(asyncio.Event):
95
+ def set(self):
96
+ self._loop.call_soon_threadsafe(super().set)
97
+
98
+ def clear(self):
99
+ self._loop.call_soon_threadsafe(super().clear)
100
+
101
+
102
+ class ThreadSafeSemaphore(asyncio.Semaphore):
103
+ pass
104
+
105
+
106
+ async def cross_thread_call(loop: asyncio.BaseEventLoop, func, *args):
107
+ done = ThreadSafeEvent()
108
+
109
+ def _helper():
110
+ func(*args)
111
+ done.set()
112
+
113
+ loop.call_soon_threadsafe(_helper)
114
+ await done.wait()
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: xasyncio
3
+ Version: 0.1
4
+ Summary: A package to publish and receive messages
5
+ Project-URL: Homepage, https://github.com/shawn-peng/xasyncio
6
+ Project-URL: Bug Tracker, https://github.com/shawn-peng/xasyncio/issues
7
+ Author-email: Yisu Peng <yisupeng@gmail.com>
8
+ License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.7
@@ -0,0 +1,5 @@
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,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Yisu Peng
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.