Skip to content

Instantly share code, notes, and snippets.

View ajitsamudrala's full-sized avatar

ajitsamudrala

View GitHub Profile
@ajitsamudrala
ajitsamudrala / delay_message.py
Last active August 20, 2019 13:26
Prints message after delaying
import logging
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
def delay_message(delay, message):
logging.info(f"{message} received")
time.sleep(delay)
@ajitsamudrala
ajitsamudrala / delay_message_w_threading.py
Last active August 20, 2019 16:13
Using python's threading module to run multiple invocations of delay_message on separate non-daemon threads
def delay_message(delay, message):
logging.info(f"{message} received")
time.sleep(delay)
logging.info(f"Printing {message}")
def main():
logging.info("Main started")
threads = [threading.Thread(target=delay_message, args=(delay, message)) for delay, message in zip([2, 3],
[num_word_mapping[2], num_word_mapping[3]])]
for thread in threads:
@ajitsamudrala
ajitsamudrala / delay_message_w_asyncio.py
Last active August 20, 2019 16:14
Use asyncio to create and launch tasks
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / delay_message_w_asyncio_gather.py
Last active March 30, 2021 09:21
Using asyncio.gather to create multiple tasks in one shot.
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / delay_message_cf.py
Created August 20, 2019 17:43
delays and prints message using ThreadPool
import concurrent.futures as cf
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / delay_message_w_blocker.py
Created August 20, 2019 18:09
A blocker task can stall the progress of the program.
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / race_condition.py
Created August 21, 2019 03:29
Race conditions can lead to unintended results.
import concurrent.futures as cf
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
class DbUpdate:
def __init__(self):
self.value = 0
@ajitsamudrala
ajitsamudrala / rece_condition_asyncio.py
Created August 21, 2019 03:37
Race conditions are race with asyncio
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
class DbUpdate:
def __init__(self):
self.value = 0
@ajitsamudrala
ajitsamudrala / race_condition_w_asyncio.py
Created August 21, 2019 03:46
both coroutines enter dead lock
import asyncio
async def foo():
await boo()
async def boo():
await foo()
async def main():
await asyncio.gather(*[foo(), boo()])
@ajitsamudrala
ajitsamudrala / threading_lock.py
Created August 21, 2019 04:04
Lock gives exclusive right to a thread.
import concurrent.futures as cf
import logging
import time
import threading
LOCK = threading.Lock()
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")