Bot freezing with Sympy (discord.py)
As Lukas Thaler said, sympy
is a synchronous library and it's not meant to be used within asynchronous code, you can however use the loop.run_in_executor
method to run it in a non-blocking way:
import asyncio
from functools import partial
loop = asyncio.get_event_loop() # in py 3.10+ use `asyncio.get_running_loop()`
async def run_blocking(func, *args, **kwargs):
"""Run any blocking, synchronous function in a non-blocking way"""
callback = partial(func, *args, **kwargs)
return await loop.run_in_executor(None, callback)
# inside the command
result = await run_blocking(sympy.sympify, calc)