Skip to content

Instantly share code, notes, and snippets.

@pwldp
Created March 9, 2018 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwldp/ddb6754a4dd4d7e6d728e991424492fe to your computer and use it in GitHub Desktop.
Save pwldp/ddb6754a4dd4d7e6d728e991424492fe to your computer and use it in GitHub Desktop.
python 3 scalable client - run workers in threads
#!/usr/bin/env python3
"""
Class for scalable client which runs workers in threads
"""
#
import random
import threading
import time
#
#
#
class ScalableClient():
#
max_workers_number = 0
curr_workers_number = 0
worker_id = 0
#
def __init__(self, max_workers_number):
self.max_workers_number = max_workers_number
#
def run(self):
while True:
self._start_worker()
time.sleep(0.1)
#
def _start_worker(self):
#print('_start_worker...')
# https://stackoverflow.com/questions/19369724/the-right-way-to-limit-maximum-number-of-threads-running-at-once
while threading.active_count() <= self.max_workers_number:
self.worker_id += 1
thread = threading.Thread(name="wrk-%05d" % self.worker_id, target=self.worker, kwargs={'id':self.worker_id})
thread.setDaemon(True)
thread.start()
#thread.join()
#
#print("threads number: %s" % threading.active_count()) # number of alive threads.
#
def worker(self, **kwargs):
run_time = random.randint(3,15)
if 'id' in kwargs:
worker_id = kwargs['id']
else:
worker_id - run_time
#
print("#%04d starting worker for %s secs" % (worker_id, run_time))
time.sleep(run_time)
print("#%04d ended worker after %s secs" % (worker_id, run_time))
#
#
#
if __name__ == "__main__":
print(__file__)
sc = ScalableClient(5)
sc.run()
#
# EOF
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment