Spawn a thread and communicate between the thread and tornado app?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with tornado/queues.py and the IOLoop.add_callback behavior linked in the issue, then compare it with run_in_executor and the standard queue API. Determine whether a Tornado-native synchronous-to-asynchronous communication pattern is feasible and what interface it should expose; done requires a decided approach, documented behavior, and tests for bidirectional communication.
Written by the indexing model from the issue text.
Description
My Tornado app needs to have bidirectional communicate with the worker thread spawned by .run_in_executor. Starting a worker thread takes time. It needs to initialise an expensive network connection before it can do the work.
Is there a native Tornado approach to solve this problem?
Approach 1: tornado.queue
I've looked into tornado.queues module, but I couldn't figure out how to use it to communicate between sync and async worlds. Documentation suggests wrapping operations with IOLoop.add_callback:
However, .add_callback is useful only for .put and .put_nowait methods, and not for .get and .get_nowait. There's no way to get the result back from .add_callback.
Approach 2: standard queue library
Next option is queue from Python standard library. It should work, but all queue operation needs to be wrapped with .run_in_executor. Seems excessive and not pretty.
Approach 3: janus library
There's janus - library for mixed sync/async queue. Looks good, but it's not small and it's an extra dependency to maintain.
Approach 4: Tornado native approach?
Is there Tornado-native approach? For example something similar to trio.from_thread.run. It's the same as IOLoop.add_callback, but Trio's run also sends back the result of the operation. Thus run can safely wrap Trio queue methods (called "memory channel" in Trio). Example from Trio documentation:
def thread_fn(receive_from_trio, send_to_trio):
while True:
# Since we're in a thread, we can't call methods on Trio
# objects directly -- so we use trio.from_thread to call them.
try:
request = trio.from_thread.run(receive_from_trio.receive)
except trio.EndOfChannel:
trio.from_thread.run(send_to_trio.aclose)
return
else:
response = request + 1
trio.from_thread.run(send_to_trio.send, response)
- Dominant language
- Python
- Stars
- 22.2k
- Forks
- 5.6k
- Avg merge
- 3h 42m
- Merged PRs (30d)
- 16
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tornadoweb/tornado
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
tornadoweb/tornado#3701 · 1 comment ·
-
httpserver
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tornadoweb/tornado#1050 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
tornadoweb/tornado#3728 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
tornadoweb/tornado#3651 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
tornadoweb/tornado#3616 · 2 comments ·
All issues in tornadoweb/tornado
Similar issues
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100