我有一个函数在我的Django应用程序中处理csvs的zip文件。我通过创建新线程并将该函数作为run方法发送来渲染视图时按下按钮后调用该函数。这是我想要调用的函数:
update_db.py
max_allowed_packet
问题是如果我在视图中调用它,该函数运行完全正常,但如果我尝试将它传递给线程则不会。这就是我在调用环境中所拥有的:
def upload_files(csv_files):
conn = sqlite3.connect('/path/to/db.db')
c = conn.cursor()
for csv_filename in csv_files.namelist():
if csv_filename.endswith('.csv'):
csv_file = csv_files.open(csv_filename)
# extract team name from csv_file string, remove whitespace
table_name = csv_filename.rsplit('/',2)[1]
table_name = re.sub('[^\w+]', '', table_name)
try:
df = pandas.read_csv(csv_file, error_bad_lines=False)
df.to_sql(table_name, conn, if_exists='append', index=False)
##do some stuff
except pandas.errors.EmptyDataError as ex:
print(str(csv_file) + ' was empty; continuing...')
continue;
conn.commit()
conn.close()
这是堆栈跟踪:
def upload(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
if str(myfile.name).endswith('.zip'):
unzipped = zipfile.ZipFile(myfile)
# If I uncomment this line, it doesn't throw an error
#upload_files(unzipped)
# When these lines execute, I get an error
t = threading.Thread(target = upload_files, args = (unzipped, ))
t.daemon = True
t.start()
return render(request, 'webapp/upload.html', {
'success': True
})
return render(request, 'webapp/upload.html', {
'success': False
})
为什么在单独的线程中运行该函数会导致错误,解决问题的最佳方法是什么?