Python 3.x python使用函数而不等待它';s返回(开火并忘记)?
我想下载文件并使用Python 3.x python使用函数而不等待它';s返回(开火并忘记)?,python-3.x,process,python-asyncio,fire-and-forget,Python 3.x,Process,Python Asyncio,Fire And Forget,我想下载文件并使用dwd功能将其保存在files文件夹中,但我不想等到文件下载完成后再运行下载功能,我想在后台运行下载功能,并使用我尝试使用的多处理功能对文件进行查看做出响应。处理未按预期工作 def files(request): # Get papram needed g_id = request.GET.get('g_id') d_id = request.GET.get('d_id') o_id = request.GET.get('o_id')
dwd
功能将其保存在files
文件夹中,但我不想等到文件下载完成后再运行下载功能,我想在后台运行下载功能,并使用我尝试使用的多处理功能对文件进行查看做出响应。处理
未按预期工作
def files(request):
# Get papram needed
g_id = request.GET.get('g_id')
d_id = request.GET.get('d_id')
o_id = request.GET.get('o_id')
m_id = request.GET.get('m_id')
download_url = (f"http://example.com/embed?video={m_id}&d=1")
r = requests.head(download_url)
content_length = r.headers['Content-length']
video_path_name = (f'streamapp/static/files/{did}_{m_id}.mp4')
if os.path.exists(video_path_name):
currnt_lenght = os.path.getsize(video_path_name)
progress = (int(currnt_lenght) *100)/ int(content_length)
progress = round(progress, 2)
else:
run_download = Process(target=dwd_file(filepath=video_path_name, download_url=download_url))
run_download.daemon = True
run_download.start()
progress = '0'
context = {'g_id': g_id, 'd_id': d_id,
'o_id': o_id, 'm_id': m_id,
'video_path_name': video_path_name,
'download_url': download_url,
'progress': progress}
return render(request, 'pages/upload.html', context)
def dwd_file(filepath, download_url):
if not os.path.exists(filepath):
r = requests.get(download_url, stream=True)
with open(filepath, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)