Я попробовал использовать модуль multiprocessing, но столкнулся с несколькими проблемами:
- Если бы я использовал multiprocessing.Process, его можно было бы завершить, но я не смог бы получить результат.
- Если бы я использовал multiprocessing.pool, он вернул бы значение, но я не смог найти способ завершить его.
Мой код должен выглядеть примерно так:
from threading import Thread
import time
def inp(prompt):
return input(prompt)
def stop():
time.sleep(1)
# close the thread
t=Thread(target=inp,args=['Enter your name: '])
t2=Thread(target=stop) # Simulate cancelling the input thread
t.start()
t2.start()
# Get the value of the return statement of t
Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-python