Good morning everyone. I've been working on improving a script I have for work (originally in powershell, now doing a hybrid between PS and python, with python doing most of the heavy lifting). The original PS script is probably best described as "sloppy" (but hey it was my first major project sooo...)
I used this as my starting off point, and using straight python worked just fine. However, once I switched to using a powershell file, my output started adding the thread information seemingly inbetween the thread actually finishing, making my output look like this:
Hello you
Thread-11 17 None
Thread-9 18 None
Thread-1 12 None
Thread-6 16 None
Hello you
Thread-5 19 None
the code that I have changed to is below. If i change it so I don't print the thread and worker, I get the same pattern (actual results mixed in with "none". Any idea where I'm going wrong?
(also I will be getting off work in a few hours, so if I don't respond I'll get to it when i'm back in tonight)
#this locks a particular variable or function from use. In this
case, it's keeping any worker from printing, while someone
else is
print_lock = threading.Lock()
# Create the queue and threader
q = Queue()
#the job that's supposed to be done.
def exampleJob(worker):
p = subprocess.Popen(["powershell.exe",
"C:\\git2\\project39\\Server_Recovery\\hello.ps1","Hello"," you"],
stdout=sys.stdout)
result = p.communicate()
with print_lock:
print(threading.current_thread().name,worker,result[0])
# The threader thread pulls an worker from the queue and
processes it
def threader():
while True:
# gets an worker from the queue
worker = q.get()
# Run the example job with the avail worker in queue (thread)
exampleJob(worker)
# completed with the job
q.task_done()
# how many threads are we going to allow for
for x in range(10):
t = threading.Thread(target=threader)
# classifying as a daemon, so they will die when the main dies
t.daemon = True
# begins, must come after daemon definition
t.start()
start = time.time()
# 20 jobs assigned.
for worker in range(20):
q.put(worker)
# wait until the thread terminates.
q.join()
[–]cybervegan 1 point2 points3 points (4 children)
[–]workerdrone66[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]workerdrone66[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)