0

I am trying to store the data received over socket while executing other functions simultaneously.

The way the code works is that it waits to receive a data over socket. If I want to receive the next data frame, I need to call recv() again which basically means that my program will stop at socket code and can not move ahead.

Is there a way to receive the data and execute other lines of code simultaneously?

Ehsan
  • 21
  • 6
  • Threading module? Asyncio? https://docs.python.org/3/library/asyncio.html – OneCricketeer Feb 03 '18 at 06:22
  • It is already built upon asyncio https://websockets.readthedocs.io/en/stable/design.html – Ehsan Feb 03 '18 at 06:31
  • @Ehsan: Is your code basically a `while` loop where you `await` a `recv` call? What do you mean by "can not move ahead"? – Blender Feb 03 '18 at 06:38
  • I am trying to think of it as hardware module in microcontroller like a timer which is working independently while CPU is executing other stuff and when the timer needs an attention, it raises an interrupt and then CPU handles the interrupt request and then goes back to where it already left. I wonder if I can have the same procedure for the web-socket data reception? – Ehsan Feb 03 '18 at 06:38
  • 1
    @Blender: I mean if I want to receive like 10 packets of data I should create a loop which exceutes the recv() 10 times: for i in range (0,10): greeting = await websocket.recv() print ('\n') print("i = {} \n < {}".format(i, greeting)) i = i + 1 – Ehsan Feb 03 '18 at 06:40
  • @Ehsan: Without more concrete information about what you're actually trying to do it's hard to give you a useful answer. You can push the execution of a task into the "background" by using `asyncio.ensure_future(task())`, which would allow you to execute code in response to your data packets without waiting for it to complete. – Blender Feb 03 '18 at 07:03
  • @Blender: Suppose that in the following example, the `long_operation` takes 100 minutes, does it mean the `task =... `goes to background and after 100 minutes the `await msg('second')` is executed? [link](https://stackoverflow.com/questions/36342899/asyncio-ensure-future-vs-baseeventloop-create-task-vs-simple-coroutine) – Ehsan Feb 03 '18 at 07:43
  • @Blender: I am trying to receive streaming data with web-sockets over several subscriptions given that the code execution does not suspend in each web-socket program and data reception goes into background while I can execute other lines of the code in the program. – Ehsan Feb 03 '18 at 07:54
  • The easiest way is using a [thread](https://docs.python.org/3/library/threading.html#threading.Thread). (Notice I said easiest, but not most efficient) – user253751 Feb 03 '18 at 11:33
  • @Ehsan: no, `long_operation` is just executed concurrently to the rest of your code. Think of it like spawning a new thread. The difference is that no code is actually being executed in parallel due to `asyncio` using an event loop. – Blender Feb 03 '18 at 17:00
  • @Blender: I removed the line `await task` in the example and expected to see `long_operation finished` but it didn't happen. Why is that? – Ehsan Feb 03 '18 at 20:54
  • @Ehsan: `loop.run_until_complete(main())` runs until `main` is complete. Since you never wait for `task` to finish, the program ends too early. – Blender Feb 03 '18 at 21:07
  • @Blender: What should I add so that main remains running and executes everything in it like printing `long_operation` and I can execute another function at the same time, say I can execute `print('test')`. – Ehsan Feb 03 '18 at 21:27
  • @Ehsan: I would condense your questions about `asyncio` into a single question and ask that separately. – Blender Feb 03 '18 at 21:49
  • @Blender: That would be great, thanks! – Ehsan Feb 03 '18 at 21:58
  • @Blender: Also, could you please let me know when you posted the question? Thanks! – Ehsan Feb 03 '18 at 22:09
  • @Ehsan: I meant for you to post the question, since I'm not sure what you don't understand from the few comments you've posted on this one. – Blender Feb 03 '18 at 22:37
  • @Blender: Got it, thanks! – Ehsan Feb 03 '18 at 22:38

0 Answers0