Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查Python中线程的活动_Python_Multithreading - Fatal编程技术网

检查Python中线程的活动

检查Python中线程的活动,python,multithreading,Python,Multithreading,我正在运行15个线程,其中线程定义通过一个类: #gpioA etc are mapped to a output port Path = collections.OrderedDict( (0,gpioA),(1,gpioB), (2,gpioC),(3,gpioD), (4,gpioE),(5,gpioF), (6,gpioG),(7,gpioH), (8,gpioI),(9,gpioJ), (10,gpioK),(11,gpioL), (12,gpioM),(13,gpioN), (14,

我正在运行15个线程,其中线程定义通过一个类:

#gpioA etc are mapped to a output port
Path = collections.OrderedDict(
(0,gpioA),(1,gpioB),
(2,gpioC),(3,gpioD),
(4,gpioE),(5,gpioF),
(6,gpioG),(7,gpioH),
(8,gpioI),(9,gpioJ),
(10,gpioK),(11,gpioL),
(12,gpioM),(13,gpioN),
(14,gpioO),(15,gpioP),))

sometime = 0.5
class myThread(threading.Thread):
    def __init__(self, id, timegap):
        self.timer = threading.Event()
        self.id = id
        self.timegap = timegap
        self.path = int(Path[id])

    def run(self):
        exit = False
        while not exit:
           self.timer.clear()
           somerandomtime = random.expovariate(1.0/self.timegap)
           exit = self.timer.wait(timeout = somerandomtime)

           if not exit:
            #change the state of a gpio pin (self.path) to low
            exit = self.timer.wait(sometime)
            #change the state of a gpio pin (self.path) to high
    def leave(self):
        self.timer.set()
在每个线程中,我将通用行的状态更改为“某时”,然后给出“某时”的间隔。 程序的主要部分,对于Path中的元素,只是在for循环中,实例化类、附加和启动线程

我想知道如何从一个线程中检查其他线程。 例如,
对于线程n(提供n>1和n<15)
,我想检查线程
(n-1)
(n+1)
是否没有执行代码的以下部分:

#change the state of a gpio pin to high
exit = self.timer.wait(sometime)
#change the state of a gpio pin to low
如果它们正在运行,线程n将跳过这部分代码。 我能想到的一种方法是,锁定gpio引脚的状态更改,将其视为一种资源,以便它仅对线程(n-1)和(n+1)不可用。问题是,这能实现吗?
除了检查线程的
Is\u alive
方法之外,还有其他方法可以调整吗?

我不能告诉你很多事情,除非看到你尝试了什么。如果你调用这个未知函数,它检测到线程X正在运行,但当它返回到你的代码时,你会如何处理这种情况,那个线程不再运行了?或者反过来说,你不想要的线程没有运行,但现在它运行了?这听起来像是一个非常脆弱的设计,需要重新思考…这通常是通过同步对象来完成的,例如
线程。锁定
。尝试重新编写或更改您的问题,以解释您试图解决的问题,而不是你的特定解决方案——对我们大多数人来说,你试图做的事情似乎没有意义,可能是因为你还不了解做你想做的事情的正确方法。总之,我已经编辑了这个问题