This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |