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 | |
from datetime import datetime | |
def _log(msg : str): | |
print(f"{datetime.utcnow()} {msg}") | |
async def dummy(name, delay_sec): | |
_log(f"{name} entering ...") | |
await asyncio.sleep(delay_sec) | |
_log(f"{name} done for the day!") |
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 | |
async def greet(name): | |
await asyncio.sleep(1) | |
print(f"Hello, {name}!") | |
async def main(): | |
task1 = asyncio.create_task(greet("Alice")) | |
task2 = asyncio.create_task(greet("Bob")) | |
await asyncio.gather(task1, task2) |
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 | |
import concurrent.futures | |
def sync_function(): | |
return "Hello from a synchronous function!" | |
async def main(): | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
result = await asyncio.get_running_loop().run_in_executor(executor, sync_function) | |
print(result) |
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 | |
async def my_coroutine(): | |
print("Hello,") | |
await asyncio.sleep(1) | |
print("world!") | |
def main(): | |
loop = asyncio.get_event_loop() | |
try: |
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
from functools import reduce | |
numbers = [1, 2, 3, 4, 5] | |
sum_result = reduce(lambda x, y: x + y, numbers) | |
print("Sum of the numbers:", sum_result) |
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
nums = [ 1,2,3,4,5 ] | |
squares = [] | |
for num in nums: | |
squares.append(num**2) | |
# instead of loop, use 'map' | |
squares = list(map(lambda num: num ** 2, nums)) |
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
''' | |
https://superfastpython.com/asyncio-semaphore/ | |
https://superfastpython.com/thread-semaphore/ | |
https://superfastpython.com/multiprocessing-semaphore-in-python/ | |
There're three versions of Semaphore: | |
from asyncio import Semaphore | |
from threading import Semaphore | |
from multiprocessing import Semaphore |
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 concurrent.futures | |
import time | |
def background_task1(): | |
item_list = [] | |
for i in range(10): | |
item_list.append(i) | |
time.sleep(1) | |
# Simulate an exception being thrown |
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 concurrent.futures | |
import time | |
def background_task(task_id): | |
item_list = [] | |
for i in range(3): | |
item_list.append((task_id, i)) | |
time.sleep(1) | |
return item_list |
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
# This is taken DIRECTLY from Geekforgeeks https://www.geeksforgeeks.org/heap-queue-or-heapq-in-python/ | |
import heapq | |
li = [5, 7, 9, 1, 3] | |
# If you call heapify, when you call heappop, order 1,3,5,7,8. If you comment out heapify call, heappop oder 5,7,9,1,3 | |
heapq.heapify(li) | |
while li: | |
print(heapq.heappop(li)) | |
# Example 2 with tuples: (w, n) where w = weight, n = node id. Heapify uses 'w' to sort first, then 'n' as tie-breaker if there are two/more elements with same 'w'. |