Does python-socketio have a start_background_task similar to flask-socketio #984
Unanswered
zhenzi0322
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Documentation link: https://python-socketio.readthedocs.io/en/latest/api.html#socketio.Server.start_background_task |
Beta Was this translation helpful? Give feedback.
1 reply
-
If you turn on the background task, you can't receive data from the server, but you can receive data after commenting out the background task import asyncio
import random
import uuid
import threading
from threading import Lock
import json
import time
from service.pick_service import PickService
import socketio
from consumer import background_thread
from config import queue
async def main():
sio = socketio.AsyncClient(reconnection=True, logger=False, engineio_logger=False, reconnection_delay=3)
sio_url = 'ws://192.168xxx:3333/?xxx'
await sio.connect(sio_url)
service = PickService(sio=sio)
@sio.on('create')
async def create_task(data: dict):
print('data', data)
job_id = data["jobId"]
queue.put(item=json.dumps(data, ensure_ascii=False))
# await asyncio.sleep(random.randint(1, 3))
await sio.emit("result", {"jobId": data["jobId"], "data": {"status": "processing"}})
sio.start_background_task(target=background_thread, service=service)
await sio.wait()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from config import queue
from service.pick_service import PickService
def background_thread(service: PickService):
p = Pool(size=20)
count = 0
while True:
count += 1
print("count", count)
value = queue.get_wait()
if value:
print(value) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How to implement start_background_task in python-socketio client?
Beta Was this translation helpful? Give feedback.
All reactions