kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

async_process.py (2396B)



      1 #
      2 # Copyright (c) 2018 Joris Vink <joris@coders.se>
      3 #
      4 # Permission to use, copy, modify, and distribute this software for any
      5 # purpose with or without fee is hereby granted, provided that the above
      6 # copyright notice and this permission notice appear in all copies.
      7 #
      8 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15 #
     16 
     17 #
     18 # Asynchronous process example.
     19 #
     20 # Wait for the result of an external process asynchronously.
     21 # The handler will execute "/bin/ls" on the current directory and
     22 # read the result.
     23 #
     24 
     25 import kore
     26 import json
     27 
     28 async def async_proc(req):
     29     #
     30     # You may specify a timeout when creating the kore.proc object.
     31     # If the timeout is reached before the process exits kore will
     32     # raise a TimeoutError exception.
     33     #
     34     # Ex: set timeout to 100ms:
     35     #   proc = kore.proc("/bin/ls -lR", 100)
     36 
     37     proc = kore.proc("/bin/ls -lR")
     38 
     39     try:
     40         stdout = ""
     41 
     42         # Read until EOF (None is returned)
     43         while True:
     44             try:
     45                 # Read from the process, with an optional 1 second timeout.
     46                 # The recv() call will throw a TimeoutError exception if
     47                 # the timeout has elapsed before any data was read.
     48                 chunk = await proc.recv(1024, 1000)
     49                 if chunk is None:
     50                     break
     51             except TimeoutError as e:
     52                 print("recv() timed out: %s" % e)
     53                 continue
     54             stdout += chunk.decode()
     55 
     56         # Reap the process.
     57         retcode = await proc.reap()
     58 
     59         # Respond with the return code + the result as JSON.
     60         payload = {
     61             "retcode": retcode,
     62             "stdout": stdout
     63         }
     64 
     65         data = json.dumps(payload, indent=4)
     66         req.response(200, data.encode())
     67     except Exception as e:
     68         # If an exception occurs we must kill the process first.
     69         proc.kill()
     70         errmsg = "Exception: %s" % e
     71         req.response(500, errmsg.encode())