[resolved] How to send data from a background worker to the frontend client? #1977
-
I want to do some computations on a worker when receiving a new connection on a HTTP endpoint and send back the result. I was able to create the HTTP endpoint, the worker, and send events from the endpoint to the worker, but not the response from the worker to the endpoint. No errors, no events, no idea of what happening. The full sample project is available here. My HTTP endpoint: class EntryPointAsyncHttpConsumer(AsyncHttpConsumer):
async def handle(self, body):
# Send headers
await self.send_headers(
headers=[
(b"Content-Type", b"text/plain"),
]
)
# Create the task
await self.channel_layer.send(
"foo",
{
"type": "foo.do_something",
"channel_name": self.channel_name,
"timestamp": datetime.datetime.now().timestamp(),
},
)
# Headers are only sent after the first body event.
await self.send_body(b"", more_body=True)
async def foo_on_something_done(self, event):
print("on_something_done", event)
await self.send_body(event["timestamp"].encode())
print("done") My worker: class WorkerSyncConsumer(SyncConsumer):
def foo_do_something(self, event):
print("do_something", event)
async_to_sync(self.channel_layer.send)(
event["channel_name"],
{"type": "foo.on_something_done", "timestamp": event["timestamp"]},
)
print("done") My asgi.py:
Web server logs:
Worker logs:
HTTP client, desperately waiting for his answer:
python & dependencies versions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Take a look at #1302. I think basically your consumer is shutting down at the end of There's a suggestion on the ticket there for adding a |
Beta Was this translation helpful? Give feedback.
Hi @jtremesay-sereema.
Take a look at #1302. I think basically your consumer is shutting down at the end of
handle()
.There's a suggestion on the ticket there for adding a
keepalive
flag to the consumer to allow processing to continue. This should get you going. I'm looking to adding something similar along these lines for the next release.