gpt4 book ai didi

Python 线程未启动

转载 作者:行者123 更新时间:2023-11-30 23:08:02 25 4
gpt4 key购买 nike

我已经创建了一个线程类,并对它们调用 start 和 join ,但是当我运行 Thread.isAlive() 时,所有这些类都返回 False。我不确定我是否正确设置了线程类,我尝试按照这篇文章 How to use threading in Python? 进行操作迈克尔·萨菲安的回答。

这是我的 Thread 类

class Thread(threading.Thread):
def __init__(self, host, communityString, deviceID, script):
super(Thread, self).__init__()
self.host = host
self.communityString=communityString
self.deviceID = deviceID
self.script=script

def CreateScript(self):
#ScriptsToGenerate = GetDatasourceScripts(deviceID)
print self.script['scriptType']
#We check if script type is SNMP and if it is we will add the oids that matter into the SNMP script
if self.script['scriptType'] == "SNMP":
print self.script['parentOID']
#walk the parent node to see if these exist
oidsToInputInScript = SNMPWalkChildren(self.host, self.communityString, self.script['OID'])
print oidsToInputInScript
if oidsToInputInScript != "":
self.script['script'].replace("[oid]", oidsToInputInScript)

SaveScript(self.host, self.script['timeInterval'], self.script)


def SaveScript(name, Interval, script):
createFolder = ""

for root, dirs, files in os.walk("/home/pi/", topdown=False):
for name in dirs:
if name == "myDir":
createFolder = os.path.join(root, name)
print createFolder

absPath = createFolder + "/" + Interval
print absPath
if not os.path.exists(absPath):
os.system("sudo mkdir " + absPath)
os.chmod(absPath, stat.S_IRWXO | stat.S_IRWXU | stat.S_IRWXG)

scriptName = name.replace(".", "")

#create script file and save it in that location
#need to replace "name" with a distinguishable name
saveFile = absPath + "/" + scriptName + "_" + script["dsID"] + "_" + script["dpID"] + ".py"
print saveFile
with open(saveFile, "w") as script_file:
os.chmod(saveFile, stat.S_IRWXO | stat.S_IRWXU | stat.S_IRWXG)
script_file.write(script["text"])


def SNMPWalkChildren(host, communityString, OID):
result = ""
try:
cmdGen = cmdgen.CommandGenerator()

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(communityString),
cmdgen.UdpTransportTarget((host, 161)),
parentOID
)

if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
result += "\"" + str(name) + "\"" + ', '
return result[:-2]
finally:
return result

这是我在线程上调用 start 的地方

threadList = list() 
#Loop through and create threads for each script
for scripts in ds:
thread = Thread(name, communityString, deviceID, scripts)
threadList.append(thread)
print "Thread has started"
print scripts
thread.start()
thread.join()

for threads in threadList:
print(threads.isAlive())

最佳答案

您在每个线程启动时加入它;这意味着它必须在循环继续之前完成。因此,当您到达第二个 for 循环时,所有线程都已完成,因此没有一个线程仍处于事件状态。

关于Python 线程未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31943018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com