• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

Thread(5) - Prod and Cons Using Queue 강의중 궁금한게 생겼습니다

21.09.20 21:46 작성 조회수 100

0

안녕하세요..강사님 패스트캠퍼스에서 부터 강의 잘 듣고 있습니다.
클래스 안에서의 동작이 잘 되질 않네요..
class main():
# 생산자
    def producer(self,queueevent):
        """네트워크 대기 상태라 가정(서버)"""
        while not event.is_set():
            message = random.randint(111)
            logging.info("Producer got message: %s"message)
            queue.put(message)

        logging.info("Producer received event. Exiting")

    # 소비자
    def consumer(self,queueevent):
        """응답 받고 소비하는 것으로 가정 or DB 저장"""
        while not event.is_set() or not queue.empty():
            message = queue.get()
            logging.info(
                "Consumer storing message: %s (size=%d)"messagequeue.qsize()
            )

        logging.info("Consumer received event. Exiting")

if __name__ == "__main__":
    # Logging format 설정
    format = "%(asctime)s%(message)s"
    logging.basicConfig(format=formatlevel=logging.INFO,
                        datefmt="%H:%M:%S")

    # 사이즈 중요
    pipeline = queue.Queue(maxsize=10)

    # 이벤트 플래그 초기 값 0
    event = threading.Event()

    # With Context 시작
    with concurrent.futures.ThreadPoolExecutor(max_workers=2as executor:
        executor.submit(main.producerpipelineevent)
        executor.submit(main.consumerpipelineevent)

        # 실행 시간 조정
        time.sleep(0.1)

        logging.info("Main: about to set event")
        
        # 프로그램 종료
        event.set()

답변 1

답변을 작성해보세요.

0

function 형식으로 바운드 하시는게 권장이니 기존 소스대로 실행 해보세요~

예외가 발생하면 로그 부분을 복사해서 붙여넣어 주시면 답변하기 편합니다.