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_lock.py (1328B)



      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 # Locking example.
     19 #
     20 # The handler for /lock will grab the shared lock, suspend itself for
     21 # 5 seconds before releasing the lock and responding.
     22 #
     23 # While the lock is held, other requests to /lock will block until it
     24 # is released.
     25 
     26 import kore
     27 
     28 # The shared lock
     29 lock = kore.lock()
     30 
     31 @kore.route("/lock", methods=["get"])
     32 async def async_lock(req):
     33     # A kore.lock should be used with the "async with" syntax.
     34     async with lock:
     35         # Suspend for 5 seconds.
     36         await kore.suspend(5000)
     37 
     38         # Now respond.
     39         req.response(200, b'')