Код: Выделить всё
import time
from threading import Thread
from multiprocessing import Process, Queue
class ProcessClasses:
def __init__(self, get_config_queue=None,):
self.get_config_queue = get_config_queue
self.get_config_queue_thread = Thread(target=self.get_config)
self.log_thread = Thread(target=self.log)
self.get_config_queue_thread.start()
# start this thread will stuck
#self.log_thread.start()
def get_config(self):
count = 0
while True:
self.get_config_queue.put(count)
print('get_config', count)
count += 1
def log(self):
#print('log')
pass
def debug():
queue = Queue(maxsize=1)
process = Process(target=ProcessClasses, args=(queue,))
process.start()
while True:
res = queue.get()
print('res', res)
print('qsize, full, empty', queue.qsize(), queue.full(), queue.empty())
time.sleep(.5)
debug_thread = Thread(target=debug)
debug_thread.start()
#debug_thread.join()
Этот код работает нормально. Но если вы запускаете поток журнала, очередь может зависать иногда.
Когда она зависает, и вы смотрите на оператор печати, очереди.full() иqueue.empty() возвращают True, что странно.
Я думаю, это какая-то проблема гонки или тупика.
Интересные выводы, временное решение, Как только я добавлю event.wait() или Оператор time.sleep() перед циклом while решил проблему.
Подробнее здесь: https://stackoverflow.com/questions/798 ... er-process
Мобильная версия