首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python线程未启动

问Python线程未启动
EN

Stack Overflow用户
提问于 2015-08-11 21:23:14
回答 1查看 42关注 0票数 1

然而,我已经创建了一个线程类,并对它们调用了start和join,然而,当我运行Thread.isAlive()时,所有它们都返回为False。我不确定我是否正确地设置了我的线程类,我尝试遵循这篇文章How to use threading in Python? Michael Safyan的答案。

这是我的Thread类

代码语言:javascript
复制
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的地方

代码语言:javascript
复制
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())
EN

回答 1

Stack Overflow用户

发布于 2015-08-11 21:24:52

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31943018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档