Replies: 4 comments 19 replies
-
I'm not sure if this is the cause of the issues you are seeing, but from a quick read of your code: @app.route('/reponse',methods = ['POST', 'GET'])
async def commande (request):
command=request.body.decode('ascii')
if command=='back' or command=='stop':
command=globals()[command]
asyncio.run(main_2(command()))
else:
command=globals()[command]
asyncio.run(main(command()))
return ("OK") should be: @app.route('/reponse',methods = ['POST', 'GET'])
async def commande (request):
command=request.body.decode('ascii')
if command=='back' or command=='stop':
command=globals()[command]
asyncio.run(main_2(command))
else:
command=globals()[command]
asyncio.run(main(command))
return ("OK") Its a little hard to follow without type hints, but it appears that |
Beta Was this translation helpful? Give feedback.
-
It looks like your program is calling When you call Then in your |
Beta Was this translation helpful? Give feedback.
-
I couldn't comment if that's pythonic. However I would structure the tasks another way. While creating and cancelling tasks is very efficient in asyncio, I found it not very clear, because I tend to loose track of which task is running, so some additional code would be needed just for housekeeping of active tasks. Also, cancelling a task is not instantaneous and may lead to having two instances of a Coro running in different tasks, especially if there is some task rundown required. This is one of the very few ways to have race conditions in asyncio, and would need careful attention. Also, there is no need for a "forward" task to run simultaneously with a "backward" task. In fact that would be an error. So I would create two tasks: one for the movement, and another one for the obstacle detection, both running always (i.e. with a The movement task's code would be something like this:
|
Beta Was this translation helpful? Give feedback.
-
No, that's not what I meant. From your original code I understood that the commands (i.e. move forward, backward) are received by a microdot action. What I propose is that the action queue the command. You will need to make the
Since
The
but then, after 10 seconds the
What I meant is this: in the obstacle detection routine, you will need to change the movement of the robot when an obstacle is detected. This could also be done by queueing commands to the movement task using, for example: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Following the discussion "Asyncio and while loop #14008", I finalized my program which works but has a bug.
If I click "Right"-"Stop"-"Forward"-"Backward" it's ok
On the other hand, "Right"-"Forward"-"Stop" the hcsr() function loops and it seems that the task.cancel() command of the main_2 function is no longer taken into account.
It's really difficult to follow the different steps of a program asyncio.
main.py
index2.html (I removed the CSS file)
and index2.js
Is there a program like pythontutor (https://pythontutor.com/visualize.html#mode=edit) to help debugging?
Thank you in advance for your help
Beta Was this translation helpful? Give feedback.
All reactions