MainThread阻止所有其他人 [英] MainThread blocks all others

查看:49
本文介绍了MainThread阻止所有其他人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我遇到了一些奇怪的情况。在MainThread中,我的程序更改了某些变量。

。这个变量应该在另一个线程中更改,但循环,

等待更改变量阻止所有其他线程。

代码如下:


class class1:

def __init __(self):

self.counter = 0

result = doSomeJob()
< br $>
def increaseCounter(个体经营):

self.counter + = 1


doSomeJob(个体经营):

#####阻止在这里###

在柜台!= 1:

通过

#...继续.. 。


#这个类订阅了一些实现线程的观察者

类监视器:

def __init __(self,klass):

#do some init

self.c = klass

def update(self):

self.c .increaseCounter()


如果__name__ ==" __主__" ;:

CL1 = 1类()

M =监视器(CL1 )

mo = MonitorObserver(m)

我很困惑如何解决这个问题。任何帮助将是

赞赏。


提前感谢所有人。


祝你好运,
/ Gelios

解决方案
Nodir Gulyamov写道:<!BLOCKQUOTE类= post_quotes>您好所有
我遇到了一些奇怪的情况。在MainThread我的程序中更改了某些变量。这个变量应该在另一个线程中更改,但是循环,
等待更改变量会阻塞所有其他线程。
下面的代码:

class class1:
def __init __( self):
self.counter = 0
结果= doSomeJob()

self.counter + = 1

doSomeJob(个体经营):
#####在这里阻止###
柜台!= 1:


应该是自我计算机吗? ?

传递
#...继续......
#这个类订阅了一些实现线程
类监视器的观察者:
def __init __(self,klass):
#do some init
self.c = klass
def update(self):
self.c.increaseCounter()

如果__name__ ==" __主__" ;:
CL1 = 1类()
米=监视(CL1)
MO = MonitorObserver(米)

我很困惑如何解决这个问题米任何帮助都将受到赞赏。




使self.counter成为一个信号量。未经测试的代码:


导入线程


class class1:

def __init __(self):

self.counter = threading.semaphore(0)

result = doSomeJob()


def increaseCounter(个体经营):

self.counter.release()


doSomeJob(个体经营):

#忙碌等待糟糕。

#而柜台!= 1:

#pass

self.counter.acquire()

#...继续...

-

--Bryan


我写道:

自我创造。一个信号量。未经测试的代码:




一点点清理。尚未经过测试:


导入线程


class class1:

def __init __(self):

self.counter = threading.semaphore(0)

result = self.doSomeJob()


def increaseCounter(self):

self.counter.release()


def doSomeJob(个体经营):

#忙碌等待糟糕。

#while counter!= 1:

#pass

self.counter.acquire()

#...继续......

-

--Bryan


你好Bryan,

谢谢你回复。

我试图测试你的解决方案,但它不起作用,因此

threading.Semaphore对象没有办法获得信号量的价值。

我查看了semaphore.py的源代码,发现该值是私有的

变量。


祝你好运,

/ Gelios


" Bryan Olson" < FA ********* @ nowhere.org>在消息中写道

news:fR ***************** @ newssvr13.news.prodigy.co m ...

我写道:

让self.counter成为一个信号量。未经测试的代码:



有点清理。尚未经过测试:

导入线程

类class1:
def __init __(self):
self.counter = threading.semaphore(0)
result = self.doSomeJob()
def increaseCounter(个体经营):
self.counter.release()

def doSomeJob(个体经营):
忙碌的等待很糟糕。
#柜台!= 1:
#pass
self.counter.acquire()
#...继续...

-
- 布莱恩



Hello All!
I met some strange situation. In MainThread my program wating changes of
some variable. This variable should be changed in another thread, but loop,
which wait changing variable blocks all other threads.
Code below:

class class1:
def __init__(self):
self.counter = 0
result = doSomeJob()

def increaseCounter(self):
self.counter += 1

doSomeJob(self):
##### BLOCKING HERE ###
while counter != 1:
pass
# ... continue...

# this class subscribed to some observer which implements thread
class monitor:
def __init__(self, klass):
#do some init
self.c = klass
def update(self):
self.c.increaseCounter()

if __name__ == "__main__":
cl1 = class1()
m = monitor(cl1)
mo = MonitorObserver(m)
I am very confused how to resolve this problem. Any help will be
appreciated.

Thanks in advance to All.

Best regards,
/Gelios

解决方案

Nodir Gulyamov wrote:

Hello All!
I met some strange situation. In MainThread my program wating changes of
some variable. This variable should be changed in another thread, but loop,
which wait changing variable blocks all other threads.
Code below:

class class1:
def __init__(self):
self.counter = 0
result = doSomeJob()

def increaseCounter(self):
self.counter += 1

doSomeJob(self):
##### BLOCKING HERE ###
while counter != 1:
Should that be self.counter?
pass
# ... continue...

# this class subscribed to some observer which implements thread
class monitor:
def __init__(self, klass):
#do some init
self.c = klass
def update(self):
self.c.increaseCounter()

if __name__ == "__main__":
cl1 = class1()
m = monitor(cl1)
mo = MonitorObserver(m)
I am very confused how to resolve this problem. Any help will be
appreciated.



Make self.counter a semaphore. Untested code:

import threading

class class1:
def __init__(self):
self.counter = threading.semaphore(0)
result = doSomeJob()

def increaseCounter(self):
self.counter.release()

doSomeJob(self):
# Busy-waiting sucks.
# while counter != 1:
# pass
self.counter.acquire()
# ... continue...
--
--Bryan


I wrote:

Make self.counter a semaphore. Untested code:



A little clean-up. Still untested:

import threading

class class1:
def __init__(self):
self.counter = threading.semaphore(0)
result = self.doSomeJob()

def increaseCounter(self):
self.counter.release()

def doSomeJob(self):
# Busy-waiting sucks.
# while counter != 1:
# pass
self.counter.acquire()
# ... continue...
--
--Bryan


Hi Bryan,
Thanks for your reply.
I tried to test your solution, but it doesn''t work, hence
threading.Semaphore object hasn''t method to get value of semaphore.
I looked to source code of semaphore.py and find out that value is private
variable.

Best regards,
/Gelios

"Bryan Olson" <fa*********@nowhere.org> wrote in message
news:fR*****************@newssvr13.news.prodigy.co m...

I wrote:

Make self.counter a semaphore. Untested code:



A little clean-up. Still untested:

import threading

class class1:
def __init__(self):
self.counter = threading.semaphore(0)
result = self.doSomeJob()

def increaseCounter(self):
self.counter.release()

def doSomeJob(self):
# Busy-waiting sucks.
# while counter != 1:
# pass
self.counter.acquire()
# ... continue...
--
--Bryan



这篇关于MainThread阻止所有其他人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆