-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
817258d
commit 83e3502
Showing
16 changed files
with
637 additions
and
547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import threading | ||
|
||
import janus | ||
|
||
|
||
class AThread(threading.Thread): | ||
"""A thread that can run async tasks.""" | ||
|
||
def __init__(self, name, awaitables=None): | ||
super().__init__(name=name, daemon=True) | ||
self._aws = list(awaitables) if awaitables is not None else [] | ||
self._lock = threading.Lock() | ||
self.__initialized = False | ||
self._stopped = False | ||
|
||
def run(self): | ||
asyncio.run(self._main()) | ||
|
||
async def _main(self): | ||
with self._lock: | ||
if self._stopped: | ||
return | ||
self._queue = janus.Queue() | ||
self.__initialized = True | ||
self._tasks = [asyncio.create_task(aw) for aw in self._aws] | ||
|
||
while True: | ||
try: | ||
aw = await self._queue.async_q.get() | ||
except BaseException: | ||
break | ||
if aw is None: | ||
break | ||
self._tasks.append(asyncio.create_task(aw)) | ||
|
||
for task in self._tasks: | ||
task.cancel() | ||
|
||
def create_task(self, awaitable): | ||
"""Create a task in the thread (thread-safe).""" | ||
with self._lock: | ||
if self.__initialized: | ||
self._queue.sync_q.put(awaitable) | ||
else: | ||
self._aws.append(awaitable) | ||
|
||
def stop(self): | ||
"""Stop the thread (thread-safe).""" | ||
with self._lock: | ||
if self.__initialized: | ||
self._queue.sync_q.put(None) | ||
else: | ||
self._stopped = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,11 @@ | ||
"""A thread for a control channel.""" | ||
from threading import Thread | ||
from .athread import AThread | ||
|
||
from tornado.ioloop import IOLoop | ||
|
||
|
||
class ControlThread(Thread): | ||
class ControlThread(AThread): | ||
"""A thread for a control channel.""" | ||
|
||
def __init__(self, **kwargs): | ||
def __init__(self): | ||
"""Initialize the thread.""" | ||
Thread.__init__(self, name="Control", **kwargs) | ||
self.io_loop = IOLoop(make_current=False) | ||
super().__init__(name="Control") | ||
self.pydev_do_not_trace = True | ||
self.is_pydev_daemon_thread = True | ||
|
||
def run(self): | ||
"""Run the thread.""" | ||
self.name = "Control" | ||
try: | ||
self.io_loop.start() | ||
finally: | ||
self.io_loop.close() | ||
|
||
def stop(self): | ||
"""Stop the thread. | ||
This method is threadsafe. | ||
""" | ||
self.io_loop.add_callback(self.io_loop.stop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.