线程等待通信

时间:2015-10-21 21:05:26

标签: python multithreading

我有一个python脚本,它在一个单独的线程上调用插件。插件必须执行一些命令并等待来自主脚本的信号才能继续。

我在插件中使用了wait()并在调用脚本中设置并清除但我想一旦调用了插件线程,主脚本会在继续之前等待线程完成。因此,永远不会调用set和wait,程序会挂起。我附上了简化版的代码。

 #!/usr/bin/python

import threading, os, sys, re, imp

e = threading.Event()

class PluginLoader():

#    atexit.register(detective.terminate())
    ## getPlugins - Locate all plugins with plugin directory
    #  @param self          Class object pointer
    #  @param moduleName    Name of module

    def getPlugins(self, moduleName):        
        try: 
            # Folder in which the plugins are stored
            self.pluginFolder = "/tmp/plugins"

            # Give the value of the main module in json file. (This is the name omitting the .py extension)
            self.mainModule = moduleName[0].strip(" ")

            # Load plugin array 
            plugins = []
            possibleplugins = os.listdir(self.pluginFolder)        

            # Iterate over plugins to determine applicable plugin
            for i in possibleplugins:              
                location=os.path.join(self.pluginFolder, i)

                # Skip if not directory or plugin not in directory
                if not os.path.isdir(location) or not self.mainModule + ".py" in os.listdir(location): 
                    continue

                # Otherwise, find the module
                info = imp.find_module(self.mainModule, [location])        
                plugins.append({"name": i, "info": info})    

            return plugins            
        except OSError:
            print "File or folder  not found"            


    ## loadPlugin - Load plugin into script
    #  @param self      Class object pointer
    #  @param plugin    Plugin object pointer
    #  @return Plugin object

    def loadPlugin(self, plugin):
        return imp.load_module(self.mainModule, *plugin["info"])


class Threads:

    def run(self):
        self.tuck()
        raw_input("press entr")            
        e.set()

    def tuck(self):
        moduleName= ["hello"]
        for i in pluginLoader.getPlugins(moduleName):
            plugin = pluginLoader.loadPlugin(i)            
            threading.Thread(name = "block", target=plugin.run(e)).start() 
            e.set()
            e.clear()       

pluginLoader=PluginLoader()    
t= Threads()
t.run()            

必须将以下脚本复制到/ tmp / plugins目录中并命名为hello.py

#!/usr/bin/python

from thre import *

class wait:
    flag = ""
    def run(self,e):
        self.flag = e
        print "in main thread"
        self.prints(e)
        e.wait()
        threading.Thread(target=self.prints12).start() 

    def prints(self,e):
        for i in xrange(10):
            print "world"

    def prints12(self):
        for i in xrange(10):
            print "Hey"


w = wait()

def run (e)            :
    w.run(e)

1 个答案:

答案 0 :(得分:0)

主要问题似乎在这里:

threading.Thread(name = "block", target=plugin.run(e)).start()

plugin.run(e)将直接调用插件的run方法并阻塞,从而使程序死锁。这可能应该是:

threading.Thread(name = "block", target=plugin.run, args=(e,)).start()