[英]what is wrong with python my python code?
1.I dont konw why “if QUEUE_HTMLNODE.qsize()>0” can be true,because i think the "if mytuple[0].qsize()>0" will never be true so the QUEUE_HTMLNODE.qsize() will never biger than zero,but the code result some times print the"jump my_tuple_list".so I am Puzzled. 1.我不知道为什么“if QUEUE_HTMLNODE.qsize()>0”可以是真的,因为我认为“if mytuple[0].qsize()>0”永远不会是真的,所以 QUEUE_HTMLNODE.qsize() 永远不会大于零,但代码结果有时会打印“跳转 my_tuple_list”。所以我很困惑。
2.Thank you for everyone help me.The full code is here: 2.谢谢大家帮我。完整的代码在这里:
#encoding=utf-8
import threading,time
import Queue
import pdb
QUEUE_HTMLNODE=Queue.Queue()
QUEUE_URLNODE=Queue.Queue()
TOTAL_COUNT=0
stop_flag=0
EXIT_FLAG=0
my_tuple_list=[]
QUEUE_URLNODE.put('way')
def single_thread(mytuple,QUEUE_HTMLNODE):
global TOTAL_COUNT
global stop_flag
global EXIT_FLAG
while stop_flag<15:
if mytuple[0].qsize()>0:
html_node="lll"
QUEUE_HTMLNODE.put(html_node)
TOTAL_COUNT+=1
else:
stop_flag+=1
time.sleep(5)
EXIT_FLAG += 1
for i in xrange(10):
my_tuple_list.append((Queue.Queue(),str(i)))
threads_list=[]
for i in xrange(10):
threads_list.append(threading.Thread(target=single_thread,args=(my_tuple_list[i],QUEUE_HTMLNODE)))
for i in threads_list:
i.setDaemon(True)
i.start()
while (True):
for i in my_tuple_list:
if QUEUE_URLNODE.qsize()>0:
node = QUEUE_URLNODE.get()
i[0].put(node)
print "in the my_tuple_list 中 "
if QUEUE_HTMLNODE.qsize()>0:
print "jump my_tuple_list"
print QUEUE_HTMLNODE.qsize()
Your issue is because you are using multiple threads, though no idea why you are using that.您的问题是因为您使用了多个线程,但不知道为什么要使用它。
To put it simply, here is the steps that is occuring -简而言之,这是正在发生的步骤-
You have created a thread which calls the function single_thread
with the values for the queue in my_tuple_list
(each element) and QUEUE_HTMLNODE
.您已经创建了调用功能的线程single_thread
与在队列中的值my_tuple_list
(每个元素)和QUEUE_HTMLNODE
。
You are then starting the thread, for first pass its fine and mytuple[0].qsize()
is 0
so nothing is added to HTMLNODE
queue.然后你开始线程,第一次通过它的罚款和mytuple[0].qsize()
是0
所以没有任何东西被添加到HTMLNODE
队列中。
But simultaneously, in the main thread , you are checking if QUERYNODE
is 0 , if it isn't you are adding elements into the queue in my_tuple_list
this is the same queues that you sent into the threads.但同时,在主线程中,您正在检查QUERYNODE
是否为 0 ,如果不是,则将元素添加到my_tuple_list
的队列中,这与您发送到线程中的队列相同。
Now, after waking up from the sleep for 5 seconds, it checks if my_tuple[0]
is still empty ( qsize
is equal to zero) , but this time it is not empty, because in main thread you put an entry inside the queue in Step 3
(above) , and since these are just threads, they share the global namespace , where the queues and lists are defined (and python's function calls are pass by reference.现在,从睡眠中醒来 5 秒钟后,它会检查my_tuple[0]
是否仍然为空( qsize
等于零),但这一次它不为空,因为在主线程中您将一个条目放入队列中Step 3
(上面),由于这些只是线程,因此它们共享全局命名空间,其中定义了队列和列表(并且 python 的函数调用是通过引用传递的。
So this time it puts entries into the QUEUE_HTMLNODE
, so the next time you go check the QUEUE_HTMLNODE
its qsize
would be greater than 0 , leading to what you observe.所以这次它把条目放入QUEUE_HTMLNODE
,所以下次你去检查QUEUE_HTMLNODE
它的qsize
将大于 0 ,导致你观察到的。
A simple example to showcase your behavior -一个简单的例子来展示你的行为 -
>>> import threading
>>> import time
>>> def func(l):
... i = 0
... while i < 15:
... l.append(i)
... i += 1
... time.sleep(1)
>>> def foo(t):
... t.start()
... i = 20
... while i > 0:
... print(l)
... i -= 1
... time.sleep(0.5)
>>> l = []
>>> t = threading.Thread(target=func, args = (l,))
>>> foo(t)
[0]
[0]
[0]
[0, 1]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.