1 回答

TA貢獻1906條經驗 獲得超10個贊
我想您會想要在線程已經發送時更新要發送的照片(不運行帶有要發送的目標照片的線程),因此您可以將要發送的照片存儲在全局變量中。這是我解決這個問題的方法:
from threading import Thread
import time
def send_email():
print('started thread')
global photos_to_send
while len(photos_to_send) > 0:
current_photo = photos_to_send.pop(0)
print('sending {}'.format(current_photo))
time.sleep(2)
print('{} sent'.format(current_photo))
print('no more photos to send ending thread')
photos_to_send = ['photo1.png']
thread1 = Thread(target=send_email, args=())
thread1.start()
photos_to_send.append('photo2.png')
thread1.join()
#started thread
#sending photo1.png
#photo1.png sent
#sending photo2.png
#photo2.png sent
#no more photos to send, ending thread
添加回答
舉報