我编写了一个API,该API以目录作为输入,它将将该文件夹(及其子文件夹)中的每个文本文件(.txt)加载到Postgres DB中。
该API适用于一些文件(大约3个),但是当它开始“读取”第四个文件时,无论文件是什么,程序都会崩溃。我什至将文件分成三个部分,API仍然停止。
我也得到一个 curl: (52) Empty reply from server
这是代码:
@apiR2A.route('/api/lectura', method=['POST'])
def read_txt():
#get_postgres_connection()
arch = [] #list of succesfully read files
arch_err = [] #list of files with errors
con = create_connection() #creating connection with db
archivos = request.query.archivos
for root, dirs, files in os.walk(archivos):
for file in files: #looping through all the files inside root
if file.endswith(".txt"): #only looking for text files
#creating dataframe
df = pd.read_csv(os.path.join(root, file), encoding="utf-8", sep="|", header=None)
if len(df.columns) is not 12: #verifying num of columns
print("WARNING: File {} has wrong format\n".format(os.path.join(root, file)))
arch_err.append(file)
continue
sleep(10)
df.columns = ["1","2","3","4","5","6","7","8","9","10","11","12"] #setting column names
print('Reading: {}'.format(os.path.join(root, file)))
#adding df to db
df.to_sql('FBDClientesCuentas',
con,
if_exists='append',
index=False)
print('{} succesfully added to db.\n'.format(file))
sleep(5)
arch.append(file)
if len(arch) > 0:
print('Files loaded to database:')
for x in arch:
print(x)
if len(arch_err) > 0:
print('\nUnread files:')
for x in arch_err:
print(x)
编辑:我忽略了似乎是问题的根源,API的运行方式如下:
if __name__ == '__main__':
apiR2A.run( server='tornado', host='0.0.0.0', port=3000, reloader=True)
问题出在我选择运行API的服务器上