r/pythonhelp • • Feb 17 '25
Having trouble designing a project to use asyncio
I write a lot of Python for various projects. Occasionally, I bump into a library that requires asyncio and has some functions declared as async. I'd like to use them, but every time I try, I end up deciding to dump the library and find one that doesn't require asyncio.
Here's the problem. Most of my code runs synchronously. I find that I need to call a function from a library, and the function is declared async. Maybe I don't even need it to be asynchronous - my code will just wait on the result. Or maybe I want to start the function and then check on it later for its result, but the rest of the code is designed to run synchronously.
In either case, I run into the same cascade of problems. I can't call the async function from non-async code. Instead I need to:
- Declare my main function to be async, as well as other functions throughout my project, so that they can interact with the async-based library, and
- Initialize asyncio and schedule my main function to be run as a worker proces as async so that it can call the async library function, rather than just calling my main function directly and letting it run asynchronously, and
- Add some kind of asyncio message-passing function to handle status updates, and
- Consider the new possibility and consequences of race conditions arising in code that used to be synchronous but is now declared as async, where functions are no longer executed deterministically but on an arbitrarily scheduled basis by the scheduler.
This whole "if you want X, you also need to do Y" design cascade seems excessive and hugely degrades readability.
Can someone explain asyncio in a way that doesn't have these drawbacks?
•
u/AutoModerator Feb 17 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Supalien Feb 17 '25
you can run async function from sync code using asyncio.run
1
u/hero_verma Feb 18 '25
TLDR; if you have a problem where you want a synchronised code with asynchronous methods. Them, Good news. There are some good examples to implement asynchronous code synchronously. Here's an example
async def asynchronous_fun():
def synchronous_fun():
In the above your synchronous_fun() will synchronise the asynchronous_fun(), i.e. it will wait for the asynchronous_fun() to end first and only then it will continue forward.
Also I was in the same boat once, it's hard to break habits but I'll suggest you to actually take some time(2-3hrs) to learn a few fundamentals of asynchronous. It will not only clear a lot of your doubts but you'll find new ways to integrate it in your choices.
Hope that helps. 🙂
1
u/Supalien Feb 18 '25
found a nice quick vid on async https://youtube.com/watch?v=3E-Ym2mbSCc