Forums

Running a <1 minute job _after_ sending reponse to the server

Hello! The header basically describes what I need; I am dealing with a server that wants a response really fast, yet I need to do some processing when I get its request. So, I need to accept data from it -> tell it "ok" -> then do a 30s job. No scheduling needed, requests never overlap, and the sending server does not care about job results.

I tried spawning a thread, but it does not do the job (seems to be killed), and then I read it's unsupported.

What should the solution be then?

there's two possibilities:

  1. your webapp writes to a db jobs queue, and then a scheduled task runs every minute to check if there are new jobs needed to be run and does that
  2. maybe within your webapp, you can try to do a async subprocess (i believe it's only threads that are not supported)

I am testing on the '/' route; to imitate the work, I print a number in a for - range loop:

def thread_test(str):
    for i in range(1000):
        print(i)
    print("YAY!")

And I cannot do just that! No matter if I use a thread, a multiprocessing.Process, a direct function call or just paste this code directly to the method, I am unable to have the loop finish: it is somewhat killed on around 340...

Doing a subprocess.Popen seems to work for me. You will have to figure it out yourself though. Also keep in mind that if it takes >> 30s (eg: 5min), the whole http response might be getting cut off (that is a hard cut off and you wouldn't be able to get around it)

That's true, subprocess works :) That's an unusual limit, given that multiprocessing module fails...

That is odd, indeed! Did you get an error relating to threads when you tried to use multiprocessing? Or was it a different message? (I'm wondering if the multiprocessing library uses threads to spawn the subprocesses or something like that.)