在会话中写入和读取视频下载进度

时间:2019-04-08 18:22:13

标签: python django pytube

除了Django视图外,我还有一个函数可以下载带有pytube库的YouTube视频。为了跟踪下载进度,我利用了pytube的{​​{1}}类YouTube参数:

on_progress_callback

yt =YouTube(url, on_progress_callback=write_download_progress) 如下:

write_download_progress

如您所见,随着下载的进行,正在向def write_download_progress(stream, chunk, file_handle, bytes_remaining): filesize = stream.filesize progress = (100*(filesize - bytes_remaining))/filesize progress = round(progress) url = 'http://127.0.0.1:8000/write_download_progress' params = {'progress': progress} try: response = requests.get(url, params) except: # ignore error caused by too frequent requests pass 视图发送请求,目的是将进度写入会话:

write_download_progress

我还有另一种观点来读取会话的进度,在下载开始之前,前端每隔两秒(到永恒)调用一次会话:

def write_download_progress(request):
    progress = request.GET.get('progress', 'Error getting progress!')
    request.session['progress'] = progress
    print('WRITE ' + str(request.session['progress']))
    return JsonResponse({'progress':'{}% downloaded...'.format(progress)})

书写视图中的def read_download_progress(request): progress = str(request.session.get('progress', 0)) print('READ ' + progress) return JsonResponse({'progress': progress}) 将打印正确的进度值。但是,阅读视图中的print(因此,该视图的JSON响应)将始终为零。

可能是什么原因?有没有更好的方法可以做到这一点,那就是将下载进度传递给后端,然后传递给前端?

0 个答案:

没有答案