Skip to content

Instantly share code, notes, and snippets.

@vishal-kvn
Created February 7, 2020 18:52
Show Gist options
  • Select an option

  • Save vishal-kvn/c3b90cbfb006d6304b6e710b71a67226 to your computer and use it in GitHub Desktop.

Select an option

Save vishal-kvn/c3b90cbfb006d6304b6e710b71a67226 to your computer and use it in GitHub Desktop.
from concurrent.futures import ThreadPoolExecutor
import csv
import numpy as np
from functools import partial
import random
import threading
import time
filename = 'lock_writes.csv'
def thread_function(lock, id):
with lock:
pause = random.randint(1, 5) / 10
print(id, ' - ', pause)
time.sleep(pause)
with open(filename, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow([threading.get_ident(), id])
filename_no_lock = 'no_lock_writes.csv'
def thread_function_no_lock(id):
pause = random.randint(1, 5) / 10
print(id, ' - ', pause)
time.sleep(pause)
with open(filename_no_lock, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow([threading.get_ident(), id])
global_lock = threading.Lock()
with open(filename, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['thread_id', 'id'])
thread_function_with_lock = partial(thread_function, global_lock)
with ThreadPoolExecutor(3) as ex:
ex.map(thread_function_with_lock, range(9))
with open(filename_no_lock, 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(['thread_id', 'id'])
with ThreadPoolExecutor(3) as ex:
ex.map(thread_function_no_lock, range(9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment