首页 \ 问答 \ Python:多处理中单个锁的死锁(Python: Deadlock of a single lock in multiprocessing)

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
更新时间:2021-10-21 20:10

最满意答案

你有很多选择。 使用momentse1071包测试偏度和峰度的两种最佳方法:

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 or e1071 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
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)