Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import threading
- from subprocess import check_output
- from queue import Queue
- NUM_THREADS = 10
- NUM_JOBS = 20
- # lock to single-thread printing
- print_lock = threading.Lock()
- # Create the queue and threader
- q = Queue()
- def exampleJob(worker):
- """Run the external program, get output and strip leading/trailing whitespace"""
- # the external program "job.sh" just waits 1 second and echos input parameters
- # we must convert the "bytes" output to a string and remove trailing '\n'
- result = check_output(['./job.sh', 'hello', 'world']).decode("utf-8").strip()
- # print the result of the job
- # with print_lock:
- # print(threading.current_thread().name, worker, result)
- # printing without locking seems OK (on MacOS, anyway)
- print(threading.current_thread().name, worker, result)
- def threader():
- """Gets a job from the queue and processes it."""
- while True:
- worker = q.get()
- exampleJob(worker)
- q.task_done()
- # Start a fixed number of threads
- for x in range(NUM_THREADS):
- t = threading.Thread(target=threader)
- t.daemon = True
- t.start()
- start = time.time()
- # generate a number of jobs and put them on the queue
- # the job is just the "job number"
- for worker in range(NUM_JOBS):
- q.put(worker)
- q.join()
- # print total elapsed time
- delta = time.time() - start
- print(f'Took {delta:.2f}s')
Advertisement
Add Comment
Please, Sign In to add comment