函数
create_file()
漫长而无聊。它创建一个约为20-25kb的JSON文件,并且没有难以使用。然后,必须使用功能
move_file()
将文件移至S3。这是代码:
# Function for moving files to s3 bucket
def move_file(file, path, bucket=c.s3cfg['bucket'], folder=c.s3cfg['folder']):
s3 = boto3.client('s3', aws_access_key_id=c.s3cfg['access_key'], aws_secret_access_key=c.s3cfg['secret_key'])
name = file.split('/')
name = folder + '/' + path + '/' + name[len(name) - 1]
try:
s3.upload_file(file, bucket, name)
os.remove(file)
except Exception as e:
get_exception(e)
threads以此为开始:
def start_thread(count=5):
NewThread(name='control').start()
for i in range(count):
name = f'thread_{i+1}'
threads[name] = NewThread(name=name)
threads[name].start()
time.sleep(0.5)
错误是错误消息:
cannot安排解释器关闭后的新期货;地点:script.py;线:49;
this ROW链接到代码中的
s3.upload_file(file, bucket, name)
。但是这个错误并非每次都显示。有时,它可以在开始错误之前将一些文件发送到服务器。 BOTO3即使在
move_file()
函数中也可以在单独的非线程脚本中运行良好。而且该代码在Python 3.8上效果很好。看来有一些全局变量关闭设置为工作过程中的某个地方。 请帮助我理解。我偶然发现了完全相同的问题,而不是boto3。 MVE:
True
您的错误很可能您不会在主线程中等待其他错误。 boto3可能需要从主线程进行操作。添加
import threading
import boto3
class worker (threading.Thread):
terminate = False
def __init__(self):
threading.Thread.__init__(self)
def run(self):
# make BOTO3 CLIENT
s3_client = boto3.client(...)
while not self.terminate:
# BOTO3 downloads from global list, not shown in this code
s3_client = boto3.download_file(...)
def stop(self):
self.terminate = True
mythread = worker()
mythread.start()
# **************** THIS IS IMPORTANT
mythread.join()
# **************** /THIS IS IMPORTANT
为我解决了。
发生错误是因为可以选择到python 3.11,但这是我建议的解决方案。
毕业到python3.12.7(推荐):
您可以升级到python 3.11,而是可以升级到
Python3.12.7,似乎已经解决了此问题。
mythread.join()
,然后重新部署您的应用程序。
确定所有threads在退出之前已正确连接。
使用python-3.12.7
使用ThreadPoolExecutor
自动管理线程关闭。
数据库连接正确关闭。