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
添加回答
举报