xasyncio 0.2.5__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.
- xasyncio-0.2.5/.github/workflows/release.yml +77 -0
- xasyncio-0.2.5/.gitignore +160 -0
- xasyncio-0.2.5/LICENSE +21 -0
- xasyncio-0.2.5/PKG-INFO +12 -0
- xasyncio-0.2.5/pyproject.toml +36 -0
- xasyncio-0.2.5/src/xasyncio/__init__.py +264 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
name: Publish Python ๐ distribution ๐ฆ to PyPI and TestPyPI
|
|
2
|
+
|
|
3
|
+
on: push
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
name: Run tests ๐งช
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
with:
|
|
13
|
+
persist-credentials: false
|
|
14
|
+
- name: Set up Python
|
|
15
|
+
uses: actions/setup-python@v5
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.x"
|
|
18
|
+
- name: Install pytest
|
|
19
|
+
run: >-
|
|
20
|
+
python3 -m
|
|
21
|
+
pip install
|
|
22
|
+
pytest
|
|
23
|
+
--user
|
|
24
|
+
- name: Install a dev package
|
|
25
|
+
run: pip install -e .
|
|
26
|
+
- name: Run tests
|
|
27
|
+
run: pytest
|
|
28
|
+
|
|
29
|
+
build:
|
|
30
|
+
name: Build distribution ๐ฆ
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
needs: test
|
|
33
|
+
|
|
34
|
+
steps:
|
|
35
|
+
- uses: actions/checkout@v4
|
|
36
|
+
with:
|
|
37
|
+
persist-credentials: false
|
|
38
|
+
- name: Set up Python
|
|
39
|
+
uses: actions/setup-python@v5
|
|
40
|
+
with:
|
|
41
|
+
python-version: "3.x"
|
|
42
|
+
- name: Install pypa/build
|
|
43
|
+
run: >-
|
|
44
|
+
python3 -m
|
|
45
|
+
pip install
|
|
46
|
+
build
|
|
47
|
+
--user
|
|
48
|
+
- name: Build a binary wheel and a source tarball
|
|
49
|
+
run: python3 -m build
|
|
50
|
+
- name: Store the distribution packages
|
|
51
|
+
uses: actions/upload-artifact@v4
|
|
52
|
+
with:
|
|
53
|
+
name: python-package-distributions
|
|
54
|
+
path: dist/
|
|
55
|
+
|
|
56
|
+
publish-to-pypi:
|
|
57
|
+
name: >-
|
|
58
|
+
Publish Python ๐ distribution ๐ฆ to PyPI
|
|
59
|
+
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
|
|
60
|
+
needs:
|
|
61
|
+
- build
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
environment:
|
|
64
|
+
name: pypi
|
|
65
|
+
url: https://pypi.org/p/xasyncio
|
|
66
|
+
permissions:
|
|
67
|
+
id-token: write # IMPORTANT: mandatory for trusted publishing
|
|
68
|
+
|
|
69
|
+
steps:
|
|
70
|
+
- name: Download all the dists
|
|
71
|
+
uses: actions/download-artifact@v4
|
|
72
|
+
with:
|
|
73
|
+
name: python-package-distributions
|
|
74
|
+
path: dist/
|
|
75
|
+
- name: Publish distribution ๐ฆ to PyPI
|
|
76
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
77
|
+
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
110
|
+
.pdm.toml
|
|
111
|
+
|
|
112
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
113
|
+
__pypackages__/
|
|
114
|
+
|
|
115
|
+
# Celery stuff
|
|
116
|
+
celerybeat-schedule
|
|
117
|
+
celerybeat.pid
|
|
118
|
+
|
|
119
|
+
# SageMath parsed files
|
|
120
|
+
*.sage.py
|
|
121
|
+
|
|
122
|
+
# Environments
|
|
123
|
+
.env
|
|
124
|
+
.venv
|
|
125
|
+
env/
|
|
126
|
+
venv/
|
|
127
|
+
ENV/
|
|
128
|
+
env.bak/
|
|
129
|
+
venv.bak/
|
|
130
|
+
|
|
131
|
+
# Spyder project settings
|
|
132
|
+
.spyderproject
|
|
133
|
+
.spyproject
|
|
134
|
+
|
|
135
|
+
# Rope project settings
|
|
136
|
+
.ropeproject
|
|
137
|
+
|
|
138
|
+
# mkdocs documentation
|
|
139
|
+
/site
|
|
140
|
+
|
|
141
|
+
# mypy
|
|
142
|
+
.mypy_cache/
|
|
143
|
+
.dmypy.json
|
|
144
|
+
dmypy.json
|
|
145
|
+
|
|
146
|
+
# Pyre type checker
|
|
147
|
+
.pyre/
|
|
148
|
+
|
|
149
|
+
# pytype static type analyzer
|
|
150
|
+
.pytype/
|
|
151
|
+
|
|
152
|
+
# Cython debug symbols
|
|
153
|
+
cython_debug/
|
|
154
|
+
|
|
155
|
+
# PyCharm
|
|
156
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
157
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
158
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
159
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
160
|
+
.idea/
|
xasyncio-0.2.5/LICENSE
ADDED
|
@@ -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.
|
xasyncio-0.2.5/PKG-INFO
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xasyncio
|
|
3
|
+
Version: 0.2.5
|
|
4
|
+
Summary: A package to simiplify multithreaded asyncio event loops
|
|
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,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[tool.hatch.version]
|
|
6
|
+
source = "vcs"
|
|
7
|
+
|
|
8
|
+
[tool.hatch.build]
|
|
9
|
+
exclude = [
|
|
10
|
+
"/tests",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[project]
|
|
14
|
+
dynamic = ["version"]
|
|
15
|
+
name = "xasyncio"
|
|
16
|
+
authors = [
|
|
17
|
+
{ name="Yisu Peng", email="yisupeng@gmail.com" },
|
|
18
|
+
]
|
|
19
|
+
description = "A package to simiplify multithreaded asyncio event loops"
|
|
20
|
+
#readme = "README.md"
|
|
21
|
+
requires-python = ">=3.7"
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
"Homepage" = "https://github.com/shawn-peng/xasyncio"
|
|
32
|
+
"Bug Tracker" = "https://github.com/shawn-peng/xasyncio/issues"
|
|
33
|
+
|
|
34
|
+
#[tool.hatch.build.targets.wheel]
|
|
35
|
+
#packages = ["src/asyncio-utils"]
|
|
36
|
+
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import dataclasses
|
|
3
|
+
import logging
|
|
4
|
+
import threading
|
|
5
|
+
import traceback
|
|
6
|
+
|
|
7
|
+
from typing import *
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ThreadingError(Exception):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def handle_result(future):
|
|
15
|
+
try:
|
|
16
|
+
# This will trigger the exception if the coroutine failed
|
|
17
|
+
future.result()
|
|
18
|
+
except Exception:
|
|
19
|
+
logging.exception("Exception caught in background thread safe task:")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclasses.dataclass
|
|
23
|
+
class AsyncThreadBase:
|
|
24
|
+
# def call_async(self, func, *args):
|
|
25
|
+
# raise NotImplementedError
|
|
26
|
+
#
|
|
27
|
+
# def call_sync(self, func, *args):
|
|
28
|
+
# raise NotImplementedError
|
|
29
|
+
#
|
|
30
|
+
# def sync_coroutine(self, coro, timeout=None):
|
|
31
|
+
# raise NotImplementedError
|
|
32
|
+
#
|
|
33
|
+
# def ensure_coroutine(self, coro):
|
|
34
|
+
# raise NotImplementedError
|
|
35
|
+
#
|
|
36
|
+
# async def await_coroutine(self, coro):
|
|
37
|
+
# raise NotImplementedError
|
|
38
|
+
#
|
|
39
|
+
# def stop(self):
|
|
40
|
+
# raise NotImplementedError
|
|
41
|
+
name: str = ''
|
|
42
|
+
loop: asyncio.AbstractEventLoop | None = None
|
|
43
|
+
events: dict = dataclasses.field(default_factory=dict)
|
|
44
|
+
events_out_thread: dict = dataclasses.field(default_factory=dict)
|
|
45
|
+
|
|
46
|
+
def _stop_self(self):
|
|
47
|
+
"""this function must be called with this thread"""
|
|
48
|
+
if self.stopped:
|
|
49
|
+
return
|
|
50
|
+
self.loop.stop()
|
|
51
|
+
self.stopped = True
|
|
52
|
+
|
|
53
|
+
async def stop(self):
|
|
54
|
+
"""thread safe stop function"""
|
|
55
|
+
# Called in other thread
|
|
56
|
+
|
|
57
|
+
# thread = threading.current_thread()
|
|
58
|
+
# if thread == self.thread:
|
|
59
|
+
# print('calling from the loop thread')
|
|
60
|
+
# else:
|
|
61
|
+
# print('calling from another loop')
|
|
62
|
+
print(f'Threaded loop {self.name} stopping')
|
|
63
|
+
await self.call_sync(self._stop_self)
|
|
64
|
+
|
|
65
|
+
def _mark_running(self, running=True):
|
|
66
|
+
# if running:
|
|
67
|
+
# print('mark running')
|
|
68
|
+
# else:
|
|
69
|
+
# print('mark stopped')
|
|
70
|
+
self.stopped = not running
|
|
71
|
+
|
|
72
|
+
def create_out_thread_event(self, name):
|
|
73
|
+
self.events_out_thread[name] = threading.Event()
|
|
74
|
+
|
|
75
|
+
def wait_out_thread_event(self, name):
|
|
76
|
+
self.events_out_thread[name].wait()
|
|
77
|
+
|
|
78
|
+
def notify_out_thread_event(self, name):
|
|
79
|
+
# print('notify event', name)
|
|
80
|
+
self.events_out_thread[name].set()
|
|
81
|
+
|
|
82
|
+
def create_event(self, name):
|
|
83
|
+
self.events[name] = threading.Event()
|
|
84
|
+
|
|
85
|
+
def notify(self, event_name):
|
|
86
|
+
self.events[event_name].set()
|
|
87
|
+
|
|
88
|
+
def wait(self, event_name):
|
|
89
|
+
self.events[event_name].wait()
|
|
90
|
+
|
|
91
|
+
# def call_blocking(self, func, *args):
|
|
92
|
+
# def call_sync(self, func, *args):
|
|
93
|
+
# # other thread will be blocked and could result in deadlocks
|
|
94
|
+
# pass
|
|
95
|
+
|
|
96
|
+
async def call_sync(self, func, *args):
|
|
97
|
+
# Called in other thread
|
|
98
|
+
# blocking_call_w_loop(self.loop, func, *args)
|
|
99
|
+
finish_event = asyncio.Event()
|
|
100
|
+
caller_loop = asyncio.get_event_loop()
|
|
101
|
+
|
|
102
|
+
def _set_finish_event():
|
|
103
|
+
caller_loop.call_soon_threadsafe(lambda: finish_event.set())
|
|
104
|
+
|
|
105
|
+
def _helper():
|
|
106
|
+
# in self thread
|
|
107
|
+
try:
|
|
108
|
+
func(*args)
|
|
109
|
+
_set_finish_event()
|
|
110
|
+
except Exception as e:
|
|
111
|
+
nonlocal exception
|
|
112
|
+
traceback.print_exc()
|
|
113
|
+
exception = e
|
|
114
|
+
_set_finish_event()
|
|
115
|
+
|
|
116
|
+
exception = None
|
|
117
|
+
|
|
118
|
+
def _ex_handler(e):
|
|
119
|
+
nonlocal exception
|
|
120
|
+
exception = e
|
|
121
|
+
raise exception
|
|
122
|
+
|
|
123
|
+
self.loop.set_exception_handler(_ex_handler)
|
|
124
|
+
|
|
125
|
+
self.loop.call_soon_threadsafe(_helper)
|
|
126
|
+
await finish_event.wait()
|
|
127
|
+
if exception:
|
|
128
|
+
raise exception
|
|
129
|
+
|
|
130
|
+
def call_async(self, func, *args):
|
|
131
|
+
self.loop.call_soon_threadsafe(func, *args)
|
|
132
|
+
|
|
133
|
+
# def _sync_coro(self, coro):
|
|
134
|
+
# if threading.current_thread() != self.thread:
|
|
135
|
+
# raise ThreadingError('Invalid thread: this function must be called in the loop thread')
|
|
136
|
+
|
|
137
|
+
def sync_coroutine(self, coro, timeout=None):
|
|
138
|
+
"""may from another thread"""
|
|
139
|
+
return asyncio.run_coroutine_threadsafe(coro, self.loop).result(timeout)
|
|
140
|
+
|
|
141
|
+
def ensure_coroutine(self, coro):
|
|
142
|
+
future = asyncio.run_coroutine_threadsafe(coro, self.loop)
|
|
143
|
+
future.add_done_callback(handle_result)
|
|
144
|
+
return future
|
|
145
|
+
|
|
146
|
+
async def run_coroutine(self, coro):
|
|
147
|
+
return await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coro, self.loop))
|
|
148
|
+
|
|
149
|
+
def blocking_call_w_loop(self, loop, func, *args):
|
|
150
|
+
pass
|
|
151
|
+
|
|
152
|
+
# def sync_coroutine_deadlock(self, coro, timeout=None):
|
|
153
|
+
# finish_event = threading.Event()
|
|
154
|
+
#
|
|
155
|
+
# async def _helper():
|
|
156
|
+
# try:
|
|
157
|
+
# await coro
|
|
158
|
+
# except Exception as e:
|
|
159
|
+
# traceback.print_exc()
|
|
160
|
+
#
|
|
161
|
+
# finish_event.set()
|
|
162
|
+
#
|
|
163
|
+
# self.loop.call_soon_threadsafe(self.loop.create_task, _helper())
|
|
164
|
+
# finish_event.wait()
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class AsyncThread(threading.Thread, AsyncThreadBase):
|
|
168
|
+
"""Class for a new thread"""
|
|
169
|
+
|
|
170
|
+
def __init__(self, name):
|
|
171
|
+
# super(AsyncThread, self).__init__()
|
|
172
|
+
threading.Thread.__init__(self)
|
|
173
|
+
AsyncThreadBase.__init__(self)
|
|
174
|
+
self.name = name
|
|
175
|
+
self.events = {}
|
|
176
|
+
self.events_out_thread = {}
|
|
177
|
+
self.loop: asyncio.BaseEventLoop | None = None
|
|
178
|
+
self.stopped = True
|
|
179
|
+
self.create_out_thread_event('loop_started')
|
|
180
|
+
self.start()
|
|
181
|
+
self.wait_out_thread_event('loop_started')
|
|
182
|
+
logging.debug('AsyncThread init finished and running')
|
|
183
|
+
|
|
184
|
+
def __repr__(self):
|
|
185
|
+
return f'<AsyncThread {self.name}>'
|
|
186
|
+
|
|
187
|
+
async def stop(self):
|
|
188
|
+
# Called in other thread
|
|
189
|
+
await super().stop()
|
|
190
|
+
res = self.join(1)
|
|
191
|
+
print(res)
|
|
192
|
+
|
|
193
|
+
def run(self):
|
|
194
|
+
self.loop = asyncio.new_event_loop()
|
|
195
|
+
asyncio.set_event_loop(self.loop)
|
|
196
|
+
|
|
197
|
+
# Need to call this in the loop, mainly because need to make sure the loop is running
|
|
198
|
+
# debugging version
|
|
199
|
+
# self.loop.call_soon_threadsafe(
|
|
200
|
+
# lambda: (
|
|
201
|
+
# print('notifying loop started'),
|
|
202
|
+
# self._mark_running(),
|
|
203
|
+
# print(self.stopped),
|
|
204
|
+
# self.notify_out_thread_event('loop_started')))
|
|
205
|
+
|
|
206
|
+
# self.loop.call_soon_threadsafe(
|
|
207
|
+
# self.loop.call_soon(
|
|
208
|
+
# lambda: (
|
|
209
|
+
# self._mark_running(), self.notify_out_thread_event('loop_started')
|
|
210
|
+
# )
|
|
211
|
+
# )
|
|
212
|
+
|
|
213
|
+
async def _notify_running():
|
|
214
|
+
logging.debug('notify running')
|
|
215
|
+
self._mark_running()
|
|
216
|
+
self.notify_out_thread_event('loop_started')
|
|
217
|
+
|
|
218
|
+
asyncio.ensure_future(_notify_running())
|
|
219
|
+
|
|
220
|
+
logging.info('running loop')
|
|
221
|
+
self.loop.run_forever()
|
|
222
|
+
print(f'Loop ({self.name}) finished')
|
|
223
|
+
|
|
224
|
+
def __hash__(self):
|
|
225
|
+
return 0
|
|
226
|
+
|
|
227
|
+
def __eq__(self, other):
|
|
228
|
+
return self is other
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class AsyncedThread(AsyncThreadBase):
|
|
232
|
+
"""Class for converting an existing thread"""
|
|
233
|
+
|
|
234
|
+
def __init__(self, name, thread):
|
|
235
|
+
self.thread = thread
|
|
236
|
+
self.loop = asyncio.get_event_loop()
|
|
237
|
+
assert self.loop.is_running() # we require the loop already running
|
|
238
|
+
self.name = name
|
|
239
|
+
self.events = {}
|
|
240
|
+
self.events_out_thread = {}
|
|
241
|
+
self.stopped = True
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class ThreadSafeEvent(asyncio.Event):
|
|
245
|
+
def set(self):
|
|
246
|
+
self._loop.call_soon_threadsafe(super().set)
|
|
247
|
+
|
|
248
|
+
def clear(self):
|
|
249
|
+
self._loop.call_soon_threadsafe(super().clear)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
class ThreadSafeSemaphore(asyncio.Semaphore):
|
|
253
|
+
pass
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
async def cross_thread_call(loop: asyncio.BaseEventLoop, func, *args):
|
|
257
|
+
done = ThreadSafeEvent()
|
|
258
|
+
|
|
259
|
+
def _helper():
|
|
260
|
+
func(*args)
|
|
261
|
+
done.set()
|
|
262
|
+
|
|
263
|
+
loop.call_soon_threadsafe(_helper)
|
|
264
|
+
await done.wait()
|