I have developed an application that works well in "asynchronous mode" and performs calculations. When the calculations meet certain criteria, a function of a "Telegram bot" is called to send the results to the bot, while the calculations continue because they are looped infinitely.
It works fine, but it's too slow.
Either my knowledge in asyncio
is insufficient, or the functionality of this library is not capable of what I need to accomplish. So, I started to think about how to speed up the process and came to the conclusion that I need to run the main chain of functions that performs the calculations in multiple processes.
However, I faced an issue where I cannot get asynchronous code to work inside the processes, no matter how hard I tried. Moreover, I need to add separate asynchronous work for the Telegram bot somewhere in my application. I have tried using multiprocessing
and aioprocessing
, but nothing seems to work.
async def main():
try:
pairs = [[..., ...], [..., ...], [..., ...], [..., ...], [..., ...]]
tasks_run = [func(pair) for pair in pairs]
await asyncio.gather(*tasks_run)
except Exception:
logger.exception('main')
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [loop.create_task(dp.start_polling()), loop.create_task(main())]
try:
loop.run_until_complete(asyncio.gather(*tasks))
except KeyboardInterrupt:
pass
finally:
for task in tasks:
task.cancel()
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
I need to create a separate process for each pair and run the coroutine func(pair) in it. Is it possible to use multiprocessing and asyncio together? If yes, how can I do that? If not, how can I solve my problem differently?
By the way, in if __name__ == '__main__'
, two tasks are created. One runs a Telegram bot, and the other runs the calculation chain. And something tells me that adding process execution will somehow break the interaction between the Telegram bot and the calculations themselves.
Please don't judge me too harshly, I've only been programming for six months.