Python:多处理中单个锁的死锁(Python: Deadlock of a single lock in multiprocessing)
我正在使用pyserial来获取多处理数据。 我共享数据的方式非常简单。 所以:
我班上有成员对象:
self.mpManager = mp.Manager() self.shared_return_list = self.mpManager.list() self.shared_result_lock = mp.Lock()
我用这种方式调用我的多处理过程:
process = mp.Process(target=do_my_stuff, args=(self.shared_stopped, self.shared_return_list, self.shared_result_lock) )
do_my_stuff
是一个全局函数。现在该部分填充了过程函数中的列表:
if len(acqBuffer) > acquisitionSpecs["LengthToPass"]: shared_lock.acquire() shared_return_list.extend(acqBuffer) del acqBuffer[:] shared_lock.release()
将本地线程使用的部分是:
while len(self.acqBuffer) <= 0 and (not self.stopped): #copy list from shared buffer and empty it self.shared_result_lock.acquire() self.acqBuffer.extend(self.shared_return_list) del self.shared_return_list[:] self.shared_result_lock.release()
问题 :
虽然只有一个锁定,但我的程序偶尔会以某种方式陷入僵局! 等了一段时间后,我的程序冻结了。 在锁之前和之后添加打印件后,我发现它在锁定时冻结并以某种方式达到死锁。
如果我使用递归锁,
RLock()
,它没有问题。 不确定我是否应该这样做。这怎么可能? 难道我做错了什么? 我希望如果两个进程都尝试获取锁,它们应该阻塞,直到另一个进程解锁。
I'm using pyserial to acquire data with multiprocessing. The way I share data is very simple. So:
I have member objects in my class:
self.mpManager = mp.Manager() self.shared_return_list = self.mpManager.list() self.shared_result_lock = mp.Lock()
I call my multiprocessing process this way:
process = mp.Process(target=do_my_stuff, args=(self.shared_stopped, self.shared_return_list, self.shared_result_lock) )
where
do_my_stuff
is a global function.Now The part the fills the list in the process function:
if len(acqBuffer) > acquisitionSpecs["LengthToPass"]: shared_lock.acquire() shared_return_list.extend(acqBuffer) del acqBuffer[:] shared_lock.release()
And the part that takes that to the local thread for use is:
while len(self.acqBuffer) <= 0 and (not self.stopped): #copy list from shared buffer and empty it self.shared_result_lock.acquire() self.acqBuffer.extend(self.shared_return_list) del self.shared_return_list[:] self.shared_result_lock.release()
The problem:
Although there's only 1 lock, my program is occasionally ending in a deadlock somehow! After waiting some time, my program freezes. After adding prints before and after the locks, I found that it freezes at a lock and reaches a deadlock somehow.
If I use a recursive lock,
RLock()
, it works with no problems. Not sure whether I should do that.How is this possible? Am I doing something wrong? I expect if both processes try to acquire the lock, they should block until the other process unlocks the lock.
原文:https://stackoverflow.com/questions/39642399
最满意答案
你有很多选择。 使用
moments
或e1071
包测试偏度和峰度的两种最佳方法:duration <- data$variable # I'm going to call it duration library(moments) kurtosis(duration) skewness(duration) library(e1071) skewness(duration) kurtosis(duration)
我应该提到偏斜和峰度几乎总是存在(只有绝对完全正态的分布才会出现)并且它们被解释为更多的渐变。 小值近似正常,值越大意味着它来自Weibull等其他分布等。
所以,你通常不会在获得p值的意义上“测试”它,就像你“测量”它并解释系数一样,看它最接近代表的分布。 话虽如此,如果你想通过使用Galton的测量而不是Pearson的测试来测试它,那么测试从零开始的显着差异。 但我不认为这会有所帮助,因为几乎所有的经验数据都会有一些显着的偏斜和峰度,因此它实际上只是一个问题(即足以使数据看起来更像是另一个分布或者是数据仍然最接近正态分布)。
如果您想使用Galton的测量,您可以找到预先打包的实现,我相信会提供它,或者执行这样的自定义函数:
galtonskew.proc <- function(x){ # # Compute Galton's skewness measure for x # NOTE: this procedure assumes no x values are missing # quarts <- as.numeric(quantile(x, probs = c(0.25, 0.5, 0.75))) num <- quarts[1] + quarts[3] - 2*quarts[2] denom <- quarts[3] - quarts[1] gskew <- num/denom gskew }
You have many options. Two of the best ways to test skewness and kurtosis using the
moments
ore1071
package:duration <- data$variable # I'm going to call it duration library(moments) kurtosis(duration) skewness(duration) library(e1071) skewness(duration) kurtosis(duration)
I should mention that skewness and kurtosis are almost always present (only in an absolutely perfectly normal distribution would it not be) and they are interpreted as more of a gradient. Small values are approximately normal and larger values mean it's from some other distribution like Weibull, etc, etc.
So, you normally don't "test" for it in the sense of getting a p-value, so much as you "measure" it and interpret the coefficients to see which distribution it most closely represents. Having said that, if you wanted to you could test for it by using Galton's measures instead of Pearson's, then testing for siginficant difference from zero. But I don't think that would be really helpful as almost all empirical data would have some significant skewness and kurtosis, thus it's really just a matter of how much (i.e. is it enough to make the data look more like another distribution or is the data still closest to the normal distribution).
In case you want to use Galton's measures you can either find a prepacked implementation, I believe
moments
provides it, or do a custom function like this:galtonskew.proc <- function(x){ # # Compute Galton's skewness measure for x # NOTE: this procedure assumes no x values are missing # quarts <- as.numeric(quantile(x, probs = c(0.25, 0.5, 0.75))) num <- quarts[1] + quarts[3] - 2*quarts[2] denom <- quarts[3] - quarts[1] gskew <- num/denom gskew }
相关问答
更多-
偏见和风雪 对于Skewness和Kurtosis(沿方差线)的在线算法,请参见同一维基页面中的高次数统计的并行算法。 中位数 没有排序数据的中位数是艰难的。 如果你知道,你有多少数据点,理论上你只需要部分排序,例如通过使用选择算法 。 但是,这并没有太多的数十亿的价值。 我建议使用频率计数,参见下一节。 频率计数的中位数和模式 如果是整数,我会计算频率 ,可能会将超出某值的最高和最低值截断,我相信它不再相关。 对于浮点数(或整数太多),我可能会创建桶/间隔,然后使用与整数相同的方法。 (近似)模式和中位数 ...
-
有两种方法可以做到这一点,但我不认为你会喜欢这个答案,因为Excel函数不能用于聚合数据。 第一种方法是展开数据。 Price 493 493 493 493 493 4500 4500 (x 7 more times, one per row) 2107 2107 (x 13 more times, one per row) 269 歪斜,峰度和标准偏差都将在展开的数据中发挥很大作用。 这是另一种方式。 扩展你的例子: Price Qty AggPrice 493 5 2465 4500 ...
-
R:面板数据的假设检验(R:hypothesis testing for panel data)[2022-07-28]
好吧,我会尝试(虽然老实说我认为你的问题更不是真正了解统计数据,而不是理解R,因此这可能不是你真正需要的,因为我不是统计学家)。 您可以使用ANOVA(使用aov函数或等效地使用lm线性回归)轻松测试每年平均值不相等的假设。我将使用后者,因为它稍后会有用。这值得做第一次因为,从逻辑上讲,如果你可以拒绝它们都相等的零假设,你可以拒绝它们都等于任何特定值的零假设。 > l1 <- lm(X ~ Year, dta) > summary(l1) Call: lm(formula = X ~ Year, data ... -
有许多方法可以估算人口时刻。 您可以使用e1071包中的偏度和峰度函数来比较这些不同的方法 > library(e1071) > skewness(a,,1) [1] 0.1277249 > skewness(a,,2) [1] 0.1514631 > skewness(a,,3) [1] 0.1090534 > kurtosis(a,,1) [1] -1.313042 > kurtosis(a,,2) [1] -1.356985 > kurtosis(a,,3) [1] -1.633564 所有这些措施 ...
-
R中有几个软件包可用来测试系数是否与0以外的值不同。例如, https://www.rforge.net/doc/packages/FSA/hoCoef.html 。 在这种情况下,您可以在hoCoef函数中使用:指定,bo = 1。 There are several packages in R that will allow you to test whether coefficients are different from values other than 0. For example, http ...
-
我需要第四个时刻吗? 或者我可以根据我的向量计算它吗? 我想你可以使用Kurtosis.evaluate()直接计算它。 do I need this fourth moment? Or can I just calculate it based on my vector? I think you can calculate it directly, using Kurtosis.evaluate().
-
你有很多选择。 使用moments或e1071包测试偏度和峰度的两种最佳方法: duration <- data$variable # I'm going to call it duration library(moments) kurtosis(duration) skewness(duration) library(e1071) skewness(duration) kurtosis(duration) 我应该提到偏斜和峰度几乎总是存在(只有绝对完全正 ...
-
这不是一个蟒蛇问题,也不是一个真正的编程问题,但答案很简单。 我们首先考虑基于较低时刻, 平均值和标准差的更容易的值,而不是倾斜和峰度。 为了使其具体,并适合您的问题,让我们假设您的数据如下: X = 3, 3, 5, 5, 5, 7 = x1, x2, x3 .... 这将给出一个看起来像“条形图”: {3:2, 5:3, 7:1} = {k1:p1, k2:p2, k3:p3} 平均值,u,由。给出 E[X] = (1/N) * (x1 + x2 + x3 + ...) = (1/N) * (3 + ...
-
基本偏度公式,Python和R之间存在不一致的偏度(Inconsistent skewness results between basic skewness formula, Python and R)[2023-11-02]
我现在无法访问您提到的软件包,所以我无法检查它们适用的公式,但是,您似乎正在使用Pearson的第二个偏斜系数(请参阅维基百科 )。 样本偏度的估计量在同一页面上给出,并由第三个时刻给出,可以通过以下方式简单计算: > S <- mean((data-mean(data))^3)/sd(data)^3 > S [1] 0.2984792 > n <- length(data) > S_alt <- S*n^2/((n-1)*(n-2)) > S_alt [1] 0.3076471 请参阅Wiki页面上的替 ... -
请使用瞬间包装中的kurtosis 。 > library(moments) > kurtosis(x) [1] 1.5 来自妈妈的kurtosis计算了皮尔逊测量峰度的估计。 实现此函数(如果x是数字向量),如下所示: n <- length(x) n * sum((x - mean(x))^4)/(sum((x - mean(x))^2)^2) 为了让你获得过多的峰度使用: > kurtosis(x)-3 [1] -1.5 现在,为了让您了解fBasics中的kurtosis什么不同,请查看代码, ...