【发布时间】:2019-10-30 05:54:57
【问题描述】:
我有一个包含日期列表的文本文件。我想将每个日期作为参数传递给 shell 脚本,并为文件中的所有指定日期运行脚本。
我想使用 python 并行执行这个任务。由于脚本具有复杂的逻辑并且要监视执行,我想一次运行 5 个实例。一旦脚本完成,python 必须启动新线程。
import threading
import time
class mythread(threading.Thread):
def __init__(self, i):
threading.Thread.__init__(self)
self.h = i
# Script will call the function
def run(self):
time.sleep(1)
print("Value send ", self.h)
f = open('C:\Senthil\SenStudy\Python\Date.txt').readlines()
num = threading.activeCount()
for i in f:
print("Active threads are ", num)
time.sleep(1)
if threading.activeCount() <= 5:
thread1 = mythread(i)
thread1.start()
else:
print("Number of Threads are More than 5 .. going to sleep state for 1 mint ...")
time.sleep(1)
我尝试使用threading.activeCount() 来获取正在运行的线程数,但从一开始它就说线程数是 30(这是文件中所有日期条目的数量)。
【问题讨论】:
标签: python