我正试图让它到达我的线程可以捕捉到信号的地方。在我看来,kill_received
单例列表位于和的相同命名空间中signal_handler()
,do_the_uploads()
并且引用了相同的内存位置。但是当我在运行时控制 C 时,我看到 False 从"print kill_received[0]"
它应该为 True 的时候开始打印,因为我点击了 control-C。
kill_received = [False]
def signal_handler(signal, frame):
global kill_received
kill_received[0] = True
print "\nYou pressed Ctrl+C!"
print (
"Your logs and their locations are:"
"\n{}\n{}\n{}".format(debug, error, info))
sys.exit(0)
def do_the_uploads(file_list, file_quantity,
retry_list, authenticate):
"""The uploading engine"""
value = raw_input(
"\nPlease enter how many conncurent "
"uploads you want at one time(example: 200)> ")
value = int(value)
logger.info('{} conncurent uploads will be used.'.format(value))
confirm = raw_input(
"\nProceed to upload files? Enter [Y/y] for yes: ").upper()
if confirm == "Y":
kill_received = False
sys.stdout.write("\x1b[2J\x1b[H")
q = Queue.Queue()
def worker():
global kill_received
while True and not kill_received[0]:
print kill_received[0]
item = q.get()
upload_file(item, file_quantity, retry_list, authenticate)
q.task_done()
for i in range(value):
t = Thread(target=worker)
t.setDaemon(True)
t.start()
for item in file_list:
q.put(item)
q.join()
print "Finished. Cleaning up processes...",
#Allowing the threads to cleanup
time.sleep(4)
print "done."
从主脚本:
from modules.upload_actions import do_the_uploads, retry, signal_handler
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
retry_list = []
file_list, authenticate, ticket_number = main()
file_quantity = FileQuantity(len(file_list))
do_the_uploads(file_list, file_quantity,
retry_list, authenticate)
更新:
仍然没有成功,但我将语法更改为这样,因为它更简洁:
def worker():
global kill_received
while not kill_received[0]:
time.sleep(1)
print kill_received[0]
item = q.get()
upload_file(item, file_quantity, retry_list, authenticate)
q.task_done()