Search code examples
pythonmultithreadingbackground-process

Python function in background


I want to run a function independently. From the function I call, I want return without waiting for the other function ending.

I tried with threadind, but this will wait, the end.

thread = threading.Thread(target=myFunc)
thread.daemon = True
thread.start()
return 'something'

Is it possible to return immediately and the other process still run? Thanks for the Answers.

EDITED The working code looks like:

 import concurrent.futures 
 executor = concurrent.futures.ThreadPoolExecutor(2) 
 executor.submit(myFunc, arg1, arg2)

Solution

  • if I understood your request correctly, you might want to take a look on worker queues https://www.djangopackages.com/grids/g/workers-queues-tasks/

    Basically it's not a good idea to offload the work to thread created in view, this is usually handled by having a pool of background workers (processes, threads) and the queue for incoming requests.