Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python falcon and async operations

I am writing an API using python3 + falcon combination.

There are lot of places in methods where I can send a reply to a client but because of some heavy code which does DB, i/o operations, etc it has to wait until the heavy part ends.

For example:

class APIHandler:                                                                      
  def on_get(self, req, resp):
    response = "Hello"
    #Some heavy code
    resp.body(response)

I could send "Hello" at the first line of code. What I want is to run the heavy code in a background and send a response regardless of when the heavy part finishes.

Falcon does not have any built-in async capabilities but they mention it can be used with something like gevent. I haven't found any documentation of how to combine those two.

like image 707
Glueon Avatar asked Dec 04 '14 14:12

Glueon


2 Answers

You can use multiprocessing.Process with deamon=True to run a daemonic process and return a response to the caller immediately:

from multiprocessing import Process

class APIHandler:

  def on_get(self, req, resp):
    heavy_process = Process(  # Create a daemonic process
        target=my_func,
        daemon=True
    )
    heavy_process.start()
    resp.body = "Quick response"


# Define some heavy function
def my_func():
    time.sleep(10)
    print("Process finished")

You can test it by sending a GET request. You will get a response immediately and, after 10s you will see a printed message in the console.

like image 133
Tomasz Bartkowiak Avatar answered Sep 27 '22 21:09

Tomasz Bartkowiak


I use Celery for async related works . I don't know about gevent .Take a look at this http://celery.readthedocs.org/en/latest/getting-started/introduction.html

like image 41
Balaji Avatar answered Sep 27 '22 19:09

Balaji