Skip to content

Instantly share code, notes, and snippets.

View narenaryan's full-sized avatar
🏠
Dreaming Big

N3N9 narenaryan

🏠
Dreaming Big
View GitHub Profile
import asyncio
# A fast co-routine
async def add_fast(x, y):
print("starting fast add")
await asyncio.sleep(3) # Mimic some network delay
print("result ready for fast add")
return x + y
# A slow co-routine
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# Create a function to schedule co-routines on the event loop
# then print results and stop the loop
async def get_results():
result1 = await add(3, 4)
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# An event loop
loop = asyncio.get_event_loop()
# Create a function to schedule co-routines on the event loop
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# An event loop
loop = asyncio.get_event_loop()
# Execute two co-routines
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# An event loop
loop = asyncio.get_event_loop()
# Pass the co-routine to the loop
# A normal function
def add(x: int, y: int):
return x + y
# A co-routine
async def add(x: int, y: int):
return x + y
CLIENT_PAGINATION_LIMIT = 200
SERVER_PAGINATION_LIMIT = 500
def main():
sql_query = 'SELECT * FROM film'
with connect(**config) as conn:
with conn.cursor("film") as cur:
# This fetches only 100 records from DB as batches
# If you don't specify, the default value is 2000
cur.itersize = SERVER_PAGINATION_LIMIT
@narenaryan
narenaryan / serversidebatch200.py
Last active January 10, 2021 18:53
cursor-illu
def main():
sql_query = 'SELECT * FROM film'
with connect(**config) as conn:
with conn.cursor("film") as cur:
# This fetches only 200 records from DB as batches
# If you don't specify, the default value is 2000
cur.itersize = 200
cur.execute(sql_query)
for row in cur:
@narenaryan
narenaryan / serverside.py
Last active January 10, 2021 17:10
cursor-illu
def main():
sql_query = 'SELECT * FROM actor WHERE first_name LIKE %s'
with connect(**config) as conn:
with conn.cursor("actor") as cur:
cur.execute(sql_query, ["John%"])
res = cur.fetchall()
print("Fetched records: %s" % res)
@narenaryan
narenaryan / fetchmany.py
Last active January 10, 2021 18:29
cursor-illu
PAGINATION_COUNT = 5
with connect(**config) as conn:
with conn.cursor() as cur:
cur.execute(sql_query, ["John%"])
res = cur.fetchmany(PAGINATION_COUNT):
# You only get 5 rows at max