Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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_Matplotlib - Fatal编程技术网

Python中的互斥

Python中的互斥,python,multithreading,matplotlib,Python,Multithreading,Matplotlib,我用python编写了一个程序,可以处理两个线程。一个线程正在从阵列打印,另一个线程正在向另一个线程正在打印的阵列添加数据。因为两个线程都在访问相同的数组,所以我决定使用一个条件变量来确保在访问所述数组时互斥。但是我忘了实现它 我同时对代码不同部分的执行时间感兴趣。因此,我使用函数time.time()来建立执行时间。当我移除测量执行时间的部件时,该功能不再工作 这使我意识到我忘记了实现条件变量。我做到了,而且成功了。然而,当我测量时间时,为什么程序在没有CV的情况下工作,我感到困惑?只是第二个

我用python编写了一个程序,可以处理两个线程。一个线程正在从阵列打印,另一个线程正在向另一个线程正在打印的阵列添加数据。因为两个线程都在访问相同的数组,所以我决定使用一个条件变量来确保在访问所述数组时互斥。但是我忘了实现它

我同时对代码不同部分的执行时间感兴趣。因此,我使用函数time.time()来建立执行时间。当我移除测量执行时间的部件时,该功能不再工作

这使我意识到我忘记了实现条件变量。我做到了,而且成功了。然而,当我测量时间时,为什么程序在没有CV的情况下工作,我感到困惑?只是第二个线程增加的执行时间降低了两个线程同时访问数组的可能性吗

在下面的代码中,我已经注释了CV的使用

import matplotlib.animation as animation
from matplotlib import style
from threading import Thread, Condition
import time
import random
import sys

style.use('fivethirtyeight')
condition = Condition()
xs = []
ys = []
i = 0
running = 1
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def getData():
    return random.randrange(1, 10, 1)

def animate(i):
    #condition.acquire()
    ax1.clear()
    ax1.plot(xs, ys)
    #condition.release()

def terminate():
    global running
    print("Goodbye")
    running = 0
    sys.exit()

class sensorHandler(Thread):
    def run(self):
        while(running):
            startTime = time.time()
            global i
            number = getData()
            #condition.acquire()
            if i<10:
                i = i+1
                xs.append(float(i))
                ys.append(float(number))
            else:
                ys[:] = ys[1:]
                ys.append(float(number))
            #condition.release()
            print(time.time()-startTime)


if __name__=="__main__":
    sensorHandler().start()
    ani = animation.FuncAnimation(fig, animate, interval=100)
    plt.show()
    terminate()´´´

将matplotlib.animation导入为动画
从matplotlib导入样式
从线程导入线程,条件
导入时间
随机输入
导入系统
style.use('fivethirtyeight'))
条件=条件()
xs=[]
ys=[]
i=0
运行=1
图=plt.图()
ax1=图add_子批次(1,1,1)
def getData():
返回random.randrange(1,10,1)
定义动画(i):
#条件获取()
ax1.clear()
ax1.绘图(xs,ys)
#条件.释放()
def terminate():
全球运行
打印(“再见”)
运行=0
sys.exit()
类sensorHandler(线程):
def运行(自):
运行时:
startTime=time.time()
全球i
number=getData()
#条件获取()

如果我很难确定,因为验证1相当困难,但我预计删除
time.time()
会暴露出依赖时间的同步失败,而执行
time.time()
会增加额外的开销

所以我相信正如你所说,“增加的执行时间是第二个线程”降低了冲突访问的可能性——一种统计上的相互排斥。当然,条件变量的正确实现可以强制确保同步

这里的多线程方案是安全的(当CV正确实现时),因为所有matplotlib操作都在一个线程中执行,但应注意,如果matplotlib操作要在多个线程之间拆分,则必须正确序列化对美工程序的访问



1潜在的验证方法往往具有破坏性-如果执行
time.time
的计算开销足以隐藏问题,则大多数正常的调试方法也会这样做

谢谢你的回复!是的,我假设matplotlib不是线程安全的。我还重写了代码,以便绘制数组的函数复制了数组,然后绘制了副本。这允许较短的CV锁定部分。