Python function in background Python function in background multithreading multithreading

Python function in background


You are more or less asking the following question:

Is it possible to run function in a subprocess without threading or writing a separate file/script

You have to change the example code from the link like this:

from multiprocessing import Processdef myFunc():    pass  # whatever function you likep = Process(target=myFunc)p.start()  # start execution of myFunc() asychronouslyprint)'something')

p.start() is executed asychronously, i.e. 'something' is printed out immediately, no matter how time consuming the execution of myFunc() is. The script executes myFunc() and does not wait for it to finish.


if I understood your request correctly, you might want to take a look on worker queueshttps://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.


I think the syntax you are using is correct and I don't see why your request shouldn't return immediately. Did you verify the request actually hang till the thread is over?

I would suggest to set myFunc to write to a file for you to track this

def myFunc():    f = open('file.txt', 'w')    while True:        f.write('hello world')