I wrote this nested loop to limit the flow of a variable. This code is based on a given array of instructions for processing. I borrowed some elements from other answers to start the stream.
import os, sys, datetime, logging, thread, threading, time from random import randint # set number of threads threadcount = 20 # alltests is an array of test data numbertests = len(alltests) testcounter = numbertests # run tests for test in alltests: # launch worker thread def worker(): """thread worker function""" os.system(command) return threads = [] t = threading.Thread(target=worker) threads.append(t) t.start() testcounter -= 1 # cap the threads if over limit while threading.active_count() >= threadcount: threads = threading.active_count() string = "Excessive threads, pausing 5 secs - " + str(threads) print (string) logging.info(string) time.sleep(5) # monitor for threads winding down while threading.active_count() != 1: threads = threading.active_count() string = "Active threads running - " + str(threads) print (string) logging.info(string) time.sleep(5)
source share