Mehrdad Dadvand Mehrdad Dadvand - 1 year ago 98
Python Question

how to check varables continuously in the loop

i've a problem with my code. it's a loop in which checks a list and if there are something in the list it will process that and remove them from list. the items inserted into the list by another thread continuously.

here is code in other thread:

while 1:
for item in list:

it seems grade so far. but cpu intensive is problem.i can put something like 'time.sleep' in the code but this program needs to process list items immediately, that's why i can't make delay in the loop

problem: since 'while 1' loop runs as fast as possible and sometimes list is empty i want to stop the loop when list is empty and continue to process when list has something

any idea how to solve that ?

Answer Source

It seems that the queue module will be useful for you. It implements multi-producer, multi-consumer queues and allows synchronize between threads without busy waiting.

see also Solution to producer and consumer problem in python which uses this queue module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download