[英]How can I pass to a function or variable the progress of a tqdm.wrapattr upload?
背景
我有一個程序,客戶端可以在其中查看服務器上的文件列表,然后選擇要從服務器上傳到 Google Cloud Storage 的文件。 當文件從服務器上傳到 Google Cloud Storage 時,我想讓客戶端知道文件上傳的進度。 至此我可以使用tqdm的wrapattr方法在服務器端的屏幕上顯示上傳的進度,如下圖:
object_address = str(uuid.uuid4())
upload_url, upload_method = get_upload_url(object_address) # fetches signed upload URL
size = os.path.getsize(filename)
with open(filename, "rb") as in_file:
total_bytes = os.fstat(in_file.fileno()).st_size
with tqdm.wrapattr(
in_file,
"read",
total=total_bytes,
miniters=1,
desc="Uploading to my bucket",
) as file_obj:
response = requests.request(
method=upload_method,
url=upload_url,
data=file_obj,
headers={"Content-Type": "application/octet-stream"},
)
response.raise_for_status()
return object_address, size
問題
我需要將上傳進度發送給客戶端。 如果您只能在服務器的終端屏幕上看到上傳的進度,則沒有任何幫助。
題
如何檢索上傳進度以便將其發送給客戶端?
不幸的是,我認為 wrapattr 函數沒有任何回調函數的參數,我可以在每次將塊上傳到 Google Cloud Storage 時調用這些參數。 我願意接受其他無需使用 tqdm wrapattr 方法即可檢索進度的方法。
我知道 Google Cloud Storage 具有可恢復的上傳,可讓您跟蹤進度,但它們使用 cURL,因此我必須使用子進程,我寧願使用比這更清潔的東西。
謝謝!
正如我所看到的,您已經能夠獲得進度並成功顯示它,但不能在客戶端。 在這個問題中,他們使用請求庫來獲取進度,試圖模擬一個類似於來自urllib2 POST 進度監控的文件適配器:
class IterableToFileAdapter(object):
def __init__(self, iterable):
self.iterator = iter(iterable)
self.length = len(iterable)
def read(self, size=-1): # TBD: add buffer for `len(data) > size` case
return next(self.iterator, b'')
def __len__(self):
return self.length
您可以使用 tqdm 將此方法應用於您自己的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.