나의 재물운? 연애운은?
AI가 봐주는 내 사주팔자 - 운세박사
----
request_queue = q.Queue()
response_queue = q.Queue()
# assign target function for the queues
# prompt 매서드를 스레드로 실행하기
request_respond = threading.Thread(target=self.prompt, args=(request_queue, response_queue))
# start the queues
request_respond.start()
# trigger the prompt request
----
The prompt function loaded as Thread
----
def prompt(self, request_queue, response_queue):
content = request_queue.get()
userid = content['userRequest']['user']['id']
content = content['action']['detailParams']['prompt']["value"]
content = ''.join(str(e) for e in content)
# run queue threading since kakaotalk chatbot will only wait 5 seconds
child_queue = q.Queue()
# create the thread
chat_gpt_respond = threading.Thread(target=self.respond, args=(child_queue, content, userid))
# Start the thread
chat_gpt_respond.start() # 스레드 시작
chat_gpt_respond.join() # 스레드가 끝날 때까지 대기
result = child_queue.get() # 스레드 종료 후 결과 값 가져오기
response_queue.put(result)
----
But I have a question, if there is nothing on request_queue, what happen to the prompt Thread?
When the prompt
thread is started, it will wait for an item to be placed in the request_queue
using the request_queue.get()
method. If there is nothing in the request_queue
, the thread will block and wait until an item becomes available.
In other words, the prompt
thread will be idle until it receives an item in the request_queue
. Once it gets an item from the queue, it will process the content and execute the remaining part of the function. If you want the thread to exit gracefully when there's nothing in the queue, you can consider using the get()
method with a timeout, like request_queue.get(timeout=5)
to wait for a certain amount of time before giving up.
정답은 request_queue가 올 때까지 기다린다입니다. 따라서, 만약 오류 발생 시 메모리 계속 잡아먹을 수 있을 것 같음. timeout 반드시 설정 필요함
나의 재물운? 연애운은?
AI가 봐주는 내 사주팔자 - 운세박사