需要一些线程模块的帮助...... [英] need some help with threading module...

查看:83
本文介绍了需要一些线程模块的帮助......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是我在这个新闻组发表的第一篇文章,我希望我的英语不太好

糟糕......
让我们直截了当!我在使用线程时遇到了一些问题:
我的小训练示例:

我希望在我的应用程序中创建两个线程,一个线程(主题)

将增加一个变量,另一个线程(控制器)将打印一条

消息,说我正在等待......或类似的东西,直到第一个线程达到10或更高的

的计数器。它将在while循环中查询第一个线程对象

关于该计数器的值,直到该计数器达到
10或更高......

这是我写的代码(它不起作用):

来自线程导入的
*


## ##################################

类主题(对象):

def __init __(个体经营):


self.counter = 0


t =计时器(0.00001, self.doIteratingStuff)


t.start()

def incrementCounter(self,n = 1):


self.counter = self.counter + n


def doIteratingStuff(self):


for i in range(10):


self.incrementCounter(4)


打印计数器现在,self.counter

def count (个体经营):


返回self.counter


################## ##################

类控制器(对象):


def __init__ (SE lf,objectToControl):


self.controlled = objectToControl


self.th =线程(target = self.stopCounting)


def start(个体经营):


self.th.start()

def stopCounting(个体经营):


i = 0

#while(1):


for i in range(100000):


print" ###############################>等待......,i


i = i + 1


如果self.controlled.count()> = 10:


打印它是十


返回


####### #############################

类应用程序(对象):


def __init __(自我):


通过


def启动(个体经营):


self.sub =主题()


self.control =控制器(self.sub)


self .control.start()

####################################

a =申请()


a.launch()


当然,因​​为我丢失了缩进从linux上我的emacs

复制了这段代码,然后将它粘贴到我的Outlook Express上(我没有网上

linux :( ...) 。


代码的问题在于控制器线程永远不会停止!!

这就是为什么while循环现在被注释并被替换为for循环。 ..我

希望问题对所有人都清楚,并且有人可以提供一些帮助!


感谢所有人和快乐的x-mas和blablablah。


法国Yacine Chaouche。

Hi all,

This is the first i post in this newsgroup, i hope my english is not too
bad...
Let''s get straight to the point ! I have a little probleme using threads in
my little training example :
I wish to create two threads in my application, one thread (the subject)
will increment a variable, and another thread (the controller) will print a
message saying "i am waiting..." or someting like that, until the counter of
the first thread reaches 10 or higher. It will query the first thread object
about the value of that counter in a while loop until that counter reaches
10 or higher...

Here is the code i wrote (it doesn''t work):

from threading import *

####################################

class Subject(object):
def __init__(self) :

self.counter = 0

t = Timer(0.00001,self.doIteratingStuff)

t.start()
def incrementCounter(self,n=1) :

self.counter = self.counter + n

def doIteratingStuff(self) :

for i in range(10) :

self.incrementCounter(4)

print "the counter is now", self.counter
def count(self):

return self.counter

####################################

class Controller(object):

def __init__(self, objectToControl) :

self.controlled = objectToControl

self.th = Thread(target = self.stopCounting)

def start(self):

self.th.start()
def stopCounting(self):

i = 0

#while(1):

for i in range(100000) :

print "###############################> waiting... ", i

i = i + 1

if self.controlled.count() >= 10 :

print "it is ten"

return

####################################

class Application (object) :

def __init__(self) :

pass

def launch(self) :

self.sub = Subject()

self.control = Controller(self.sub)

self.control.start()
####################################
a = Application()

a.launch()

Of course, the indentations are lost since i copied this code from my emacs
on linux, and then paste it on my outlook express (i don''t have the net on
linux :(...).

The probleme with the code is that the controler thread never stops !!
that''s why the while loop is now commented and replaced by a for loop... I
wish the problem is clear for all and that somebody can give some help !

Thanks for all and merry x-mas and blablablah.

Yacine Chaouche, France.

推荐答案

>谢谢大家和快乐的x-mas和blablablah

圣诞节没有X,blablablah应该读新年快乐!
> Thanks for all and merry x-mas and blablablah
There is no X in Christmas, and blablablah should read Happy New Year!
当然,缩进因为我从我的
emacson linux复制了这段代码,然后将它粘贴到我的Outlook Express上(我没有
net onlinux :( ...)。
Of course, the indentations are lost since i copied this code from my emacson linux, and then paste it on my outlook express (i don''t have the net onlinux :(...).




我之前有空白被剥离的问题,但我只是

无法理解为什么你会发布知道它是否缺少缩进。

修复它重新发布,也许有人可能会帮助你。


Hth,

MEFarmer



I have had problems with whitespace being stripped before, but I just
cannot understand WHY would you post KNOWING it was missing indents.
Fix-it repost and maybe someone might help you.

Hth,
M.E.Farmer


我认为它更适合这种形式...

来自线程导入的
*


#### ################################

类主题(对象):

def __init __(自我):


self.counter = 0


t =计时器(0.00001,自我。 doIteratingStuff)


t。 start()

def incrementCounter(self,n = 1):


self.counter = self.counter + n


def doIteratingStuff(self):


for i in range(10):


self.incrementCounter(4)


打印计数器现在,self.counter

def count(个体经营):


返回自我.counter


####################################


类控制器(对象):


def __init __(self,objectToControl):


self.controlled = objectToControl


self.th =线程(target = self.stopCounting)

def start(self):


self.th.start()

def stopCounting(个体经营):


i = 0


#while(1):


for i in range(100000):


print" ### ############################>等待......,i


i = i + 1


如果self.controlled.count()> = 10:


打印它是十


返回


####### #############################

类应用程序(对象):


def __init __(自我):


通过


def启动(个体经营):


self.sub =主题()


self.control =控制器(self.sub)


self .control.start()

####################################

a =申请()


a.launch()


为所有人提供服务。


法国Yacine Chaouche。
I think it is more suitable in this form...

from threading import *

####################################

class Subject(object):
def __init__(self) :

self.counter = 0

t = Timer(0.00001,self.doIteratingStuff)

t.start()
def incrementCounter(self,n=1) :

self.counter = self.counter + n

def doIteratingStuff(self) :

for i in range(10) :

self.incrementCounter(4)

print "the counter is now", self.counter
def count(self):

return self.counter

####################################

class Controller(object):

def __init__(self, objectToControl) :

self.controlled = objectToControl

self.th = Thread(target = self.stopCounting)

def start(self):

self.th.start()
def stopCounting(self):

i = 0

#while(1):

for i in range(100000) :

print "###############################> waiting... ", i

i = i + 1

if self.controlled.count() >= 10 :

print "it is ten"

return

####################################

class Application (object) :

def __init__(self) :

pass

def launch(self) :

self.sub = Subject()

self.control = Controller(self.sub)

self.control.start()
####################################
a = Application()

a.launch()

Thanls for all.

Yacine Chaouche, France.




只是一个警告!

线程和新手不要好好混合,

很多陷阱,很难找到等待你的bug。

如果可能的话我会避免使用线程。

现在我们已经完成所有这些让我们看到一些代码了。


py>导入线程

py> class Test(threading.Thread):

.... def run(self):

.... x = 0

。 ......而x < 10:

....打印x

.... x + = 1

.... print"结束

像这样使用:

py>测试()。开始()


还有其他方法可以做到这一点,但你需要在

threading.py上进行网络搜索,有很多例子。

hth,

MEFarmer


Just a warning!
Threads and newbies don''t mix well,
many pitfalls and hard to find bugs await you.
I would avoid using threads if at all possible.

Now we have all that over lets see some code.

py> import threading
py> class Test(threading.Thread):
.... def run(self):
.... x = 0
.... while x < 10:
.... print x
.... x += 1
.... print " Ending"
Use it like this:
py> Test().start()

There are other ways to do this, but you need to do a web search on
threading.py , there are many examples out there.
hth,
M.E.Farmer


这篇关于需要一些线程模块的帮助......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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