kopia lustrzana https://github.com/cirospaciari/socketify.py
added cork_end helper
rodzic
bce55b4c0f
commit
43993449c5
|
@ -605,6 +605,9 @@ class AppResponse:
|
||||||
except:
|
except:
|
||||||
return (False, False)
|
return (False, False)
|
||||||
|
|
||||||
|
def cork_end(self, message, end_connection=False):
|
||||||
|
self.cork(lambda res: res.end(message, end_connection))
|
||||||
|
|
||||||
def end(self, message, end_connection=False):
|
def end(self, message, end_connection=False):
|
||||||
try:
|
try:
|
||||||
if self.aborted:
|
if self.aborted:
|
||||||
|
|
|
@ -5,7 +5,7 @@ app = App()
|
||||||
|
|
||||||
async def delayed_hello(delay, res):
|
async def delayed_hello(delay, res):
|
||||||
await asyncio.sleep(delay) #do something async
|
await asyncio.sleep(delay) #do something async
|
||||||
res.cork(lambda res: res.end("Hello with delay!"))
|
res.cork_end("Hello with delay!")
|
||||||
|
|
||||||
def home(res, req):
|
def home(res, req):
|
||||||
#request object only lives during the life time of this call
|
#request object only lives during the life time of this call
|
||||||
|
@ -22,7 +22,8 @@ async def json(res, req):
|
||||||
user_agent = req.get_header("user-agent")
|
user_agent = req.get_header("user-agent")
|
||||||
#req maybe will not be available in direct attached async functions after await
|
#req maybe will not be available in direct attached async functions after await
|
||||||
await asyncio.sleep(2) #do something async
|
await asyncio.sleep(2) #do something async
|
||||||
res.cork(lambda res: res.end({ "message": "I'm delayed!", "user-agent": user_agent}))
|
|
||||||
|
res.cork_end({ "message": "I'm delayed!", "user-agent": user_agent})
|
||||||
|
|
||||||
def not_found(res, req):
|
def not_found(res, req):
|
||||||
res.write_status(404).end("Not Found")
|
res.write_status(404).end("Not Found")
|
||||||
|
|
|
@ -45,7 +45,7 @@ def list_original_pokemons(res, req):
|
||||||
#get asynchronous from Model
|
#get asynchronous from Model
|
||||||
async def get_originals():
|
async def get_originals():
|
||||||
value = await cache.run_once("original_pokemons", 5, get_original_pokemons)
|
value = await cache.run_once("original_pokemons", 5, get_original_pokemons)
|
||||||
res.cork(lambda res: res.end(value))
|
res.cork_end(value)
|
||||||
|
|
||||||
res.run_async(get_originals())
|
res.run_async(get_originals())
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ def list_pokemon(res, req):
|
||||||
#sync with redis lock to run only once
|
#sync with redis lock to run only once
|
||||||
#if more than 1 worker/request try to do this request, only one will call the Model and the others will get from cache
|
#if more than 1 worker/request try to do this request, only one will call the Model and the others will get from cache
|
||||||
value = await cache.run_once(cache_key, 5, get_pokemon, number)
|
value = await cache.run_once(cache_key, 5, get_pokemon, number)
|
||||||
res.cork(lambda res: res.end(value))
|
res.cork_end(value)
|
||||||
|
|
||||||
res.run_async(find_pokemon(number, res))
|
res.run_async(find_pokemon(number, res))
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,10 @@ def user(res, req):
|
||||||
|
|
||||||
async def delayed_hello(delay, res):
|
async def delayed_hello(delay, res):
|
||||||
await asyncio.sleep(delay) #do something async
|
await asyncio.sleep(delay) #do something async
|
||||||
res.cork(lambda res: res.end("Hello sorry for the delay!"))
|
res.cork_end("Hello sorry for the delay!")
|
||||||
|
# cork_end is a less verbose way of writing
|
||||||
|
# res.cork(lambda res: res.end("Hello sorry for the delay!"))
|
||||||
|
|
||||||
|
|
||||||
def delayed(res, req):
|
def delayed(res, req):
|
||||||
#request object only lives during the life time of this call
|
#request object only lives during the life time of this call
|
||||||
|
@ -66,7 +69,7 @@ async def sleepy_json(res, req):
|
||||||
#req maybe will not be available in direct attached async functions after await
|
#req maybe will not be available in direct attached async functions after await
|
||||||
#but if you dont care about req info you can do it
|
#but if you dont care about req info you can do it
|
||||||
await asyncio.sleep(2) #do something async
|
await asyncio.sleep(2) #do something async
|
||||||
res.cork(lambda res: res.end({ "message": "I'm delayed!", "user-agent": user_agent}))
|
res.cork_end({ "message": "I'm delayed!", "user-agent": user_agent})
|
||||||
|
|
||||||
def custom_header(res, req):
|
def custom_header(res, req):
|
||||||
res.write_header("Content-Type", "application/octet-stream")
|
res.write_header("Content-Type", "application/octet-stream")
|
||||||
|
|
|
@ -10,7 +10,7 @@ def upload(res, req):
|
||||||
def on_data(res, chunk, is_end):
|
def on_data(res, chunk, is_end):
|
||||||
print(f"Got chunk of data with length {len(chunk)}, is_end: {is_end}")
|
print(f"Got chunk of data with length {len(chunk)}, is_end: {is_end}")
|
||||||
if (is_end):
|
if (is_end):
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
res.on_data(on_data)
|
res.on_data(on_data)
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ async def upload_chunks(res, req):
|
||||||
print(f"Got chunk of data with length {len(chunk)}")
|
print(f"Got chunk of data with length {len(chunk)}")
|
||||||
|
|
||||||
#We respond when we are done
|
#We respond when we are done
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
async def upload_json(res, req):
|
async def upload_json(res, req):
|
||||||
print(f"Posted to {req.get_url()}")
|
print(f"Posted to {req.get_url()}")
|
||||||
|
@ -35,7 +35,7 @@ async def upload_json(res, req):
|
||||||
print(f"First person is named: {people[0]['name']}")
|
print(f"First person is named: {people[0]['name']}")
|
||||||
|
|
||||||
#We respond when we are done
|
#We respond when we are done
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
async def upload_text(res, req):
|
async def upload_text(res, req):
|
||||||
print(f"Posted to {req.get_url()}")
|
print(f"Posted to {req.get_url()}")
|
||||||
|
@ -45,7 +45,7 @@ async def upload_text(res, req):
|
||||||
print(f"Your text is ${text}")
|
print(f"Your text is ${text}")
|
||||||
|
|
||||||
#We respond when we are done
|
#We respond when we are done
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
async def upload_urlencoded(res, req):
|
async def upload_urlencoded(res, req):
|
||||||
print(f"Posted to {req.get_url()}")
|
print(f"Posted to {req.get_url()}")
|
||||||
|
@ -55,7 +55,7 @@ async def upload_urlencoded(res, req):
|
||||||
print(f"Your form is ${form}")
|
print(f"Your form is ${form}")
|
||||||
|
|
||||||
#We respond when we are done
|
#We respond when we are done
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
|
|
||||||
async def upload_multiple(res, req):
|
async def upload_multiple(res, req):
|
||||||
|
@ -72,7 +72,7 @@ async def upload_multiple(res, req):
|
||||||
print(f"Your data is ${data}")
|
print(f"Your data is ${data}")
|
||||||
|
|
||||||
#We respond when we are done
|
#We respond when we are done
|
||||||
res.cork(lambda res: res.end("Thanks for the data!"))
|
res.cork_end("Thanks for the data!")
|
||||||
|
|
||||||
|
|
||||||
app = App()
|
app = App()
|
||||||
|
|
Ładowanie…
Reference in New Issue