Skip to content

Instantly share code, notes, and snippets.

@prmishra
Last active April 2, 2019 08:40
Show Gist options
  • Save prmishra/281f0372c9b97c380e6edd81309b3e4a to your computer and use it in GitHub Desktop.
Save prmishra/281f0372c9b97c380e6edd81309b3e4a to your computer and use it in GitHub Desktop.
import os
import sys
from threading import Thread
class Counter(object):
def __init__(self):
self.count = 0
def increment(self, offset):
self.count += offset
def worker(sensor_index, count_per_thread, counter):
for _ in range(count_per_thread):
counter.increment(1)
def run_threads(func, num_of_threads, count_per_thread, counter):
threads = []
for i in range(num_of_threads):
args = (i, count_per_thread, counter)
thread = Thread(target=func, args=args)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
def print_usage():
print("Usage:- python {} num_of_threads count_per_thread".format(
os.path.basename(__file__),
))
def parse_arg():
try:
return int(sys.argv[1]), int(sys.argv[2])
except ValueError:
print_usage()
exit(1)
if __name__ == "__main__":
if len(sys.argv) == 3:
num_of_threads, count_per_thread = parse_arg()
counter = Counter()
run_threads(worker, num_of_threads, count_per_thread, counter)
print('Counter should be %d, found %d' % (
num_of_threads * count_per_thread, counter.count,
))
else:
print_usage()
exit(1)
# python script.py 5 100000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment