Find answers from the community

Updated last month

Using a Pool to Run a Long-Running Function in the Background Without Blocking the Main Thread

At a glance

The community member has a chatbot with a custom tool and wants to start a background process that takes time, and send a message to the user. They tried using multiprocessing.Pool but the long-running function was never called. A comment suggests that if the function/code is asynchronous, the community member should use asyncio.create_task instead of multiprocessing.

Hi everyone! I have a chatbot with a tool (created using class CustomToolSpec(BaseToolSpec)), and want to start a background process thats take time, and send just a message to user... I already try something like:

Plain Text
        with multiprocessing.Pool(processes=1) as pool:
                future = pool.apply_async(self.my_long_function, args=(my_args,))
        return "Thanks, I will start the long-function."


but my_long_function are never called. Any way to accomplish this?
L
1 comment
if your function/code is async, I would just use asyncio.create_task instead of messing around with multiprocessing
Add a reply
Sign up and join the conversation on Discord