6

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)
  • Did you have this code inside another function that would be acting as an inline function? (the one that would end before `thread` finishes) – Generic Snake Feb 12 '16 at 08:11
  • This is a Django view function, and I want to redirect before ending the called function, but this wait. – Benjámin Gerván Feb 12 '16 at 08:14
  • You have to offload the function to another thread. What you run was working on the same thread. – Aviah Laor Feb 12 '16 at 08:41
  • Possible duplicate of [Is it possible to run function in a subprocess without threading or writing a separate file/script](http://stackoverflow.com/questions/2046603/is-it-possible-to-run-function-in-a-subprocess-without-threading-or-writing-a-se) – Johann Hagerer Feb 12 '16 at 08:47
  • I see that ticket before I post this. That didn't solve my probleme. – Benjámin Gerván Feb 12 '16 at 09:13

3 Answers3

4

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 Process

def myFunc():
    pass  # whatever function you like

p = Process(target=myFunc)
p.start()  # start execution of myFunc() asychronously
print)'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.

Johann Hagerer
  • 1,048
  • 2
  • 10
  • 28
2

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.

  • Thanks, with this I get to executors. The working code looks like: import concurrent.futures executor = concurrent.futures.ThreadPoolExecutor(2) executor.submit(myFunc, arg1, arg2) – Benjámin Gerván Feb 12 '16 at 09:02
0

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')
Forge
  • 6,538
  • 6
  • 44
  • 64