运行 在后台运行 class 函数并检索其状态
Run a class function in background and retrieve its status
import threading
from azure.storage.blob import BlockBlobService
def do_other_stuff():
print("so much stuff to do")
class ABlob:
def __init__(self, account_name, account_key, container_name, blob_name, file_path):
self.account_name = account_name
self.account_key = account_key
self.container_name = container_name
self.blob_name = blob_name
self.file_path = file_path
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=self.account_key)
def get_blob(self):
download_thread = threading.Thread(
target=self.blob_service.get_blob_to_path,
args=(self.container_name, self.blob_name, self.file_path))
download_thread.start()
def get_blob_name(self):
print(self.blob_name)
first_blob = ABlob(account_name='account_name',
account_key='key',
container_name='container', blob_name='something.csv',
file_path='path')
first_blob.get_blob()
first_blob.get_blob_name()
do_other_stuff()
我有需要下载和上传的 Azure Blob(未显示)。我不想等待他们完成他们的流程,因为我还有其他应该做的事情。不过在某些时候,我需要确认他们是否已成功下载或上传。
在我目前的代码中,我已经使用了线程库。如果在上传或下载过程中发生错误,处理事务的线程将错误退出。我没有办法通知主线程完成和完成的状态。
我需要做什么才能获得 get_blob
的状态?是否有另一个库可以用一种不那么危险的方式来处理这种情况?我参考了以下线程,但无法弄清楚如何结合它们的不同方法。
Catch a thread's exception in the caller thread in Python
python multiprocessing pool retries
background function in Python
What do I need to do to be able to get the status of get_blob
?
您可以将 get_blob
包装在一个函数中,该函数将存储有关它是否成功的信息,并存储 return 值(如果有)。您可以写 target=self._get_blob_background
而不是 target=self.blob_service.get_blob_to_path
。新的 _get_blob_background
方法可以调用 self.result = self.blob_service.get_blob_to_path
并使用 try
和 except Exception as e
来捕获所有异常,并在异常情况下执行 self.result_exception = e
,以便主要线程可以区分结果和异常。
更好的是,您可以使用 concurrent.futures
库为您完成所有这些工作:
pool = concurrent.futures.ThreadPoolExecutor()
def get_blob(self):
return pool.submit(self.blob_service.get_blob_to_path,
self.container_name, self.blob_name, self.file_path)
现在 get_blob()
将在后台 运行,就像在您的代码中一样,但这里它将 return 一个 Future
对象,您可以查询该对象以查明是否作业已完成,以及如何完成。
import threading
from azure.storage.blob import BlockBlobService
def do_other_stuff():
print("so much stuff to do")
class ABlob:
def __init__(self, account_name, account_key, container_name, blob_name, file_path):
self.account_name = account_name
self.account_key = account_key
self.container_name = container_name
self.blob_name = blob_name
self.file_path = file_path
self.blob_service = BlockBlobService(account_name=self.account_name, account_key=self.account_key)
def get_blob(self):
download_thread = threading.Thread(
target=self.blob_service.get_blob_to_path,
args=(self.container_name, self.blob_name, self.file_path))
download_thread.start()
def get_blob_name(self):
print(self.blob_name)
first_blob = ABlob(account_name='account_name',
account_key='key',
container_name='container', blob_name='something.csv',
file_path='path')
first_blob.get_blob()
first_blob.get_blob_name()
do_other_stuff()
我有需要下载和上传的 Azure Blob(未显示)。我不想等待他们完成他们的流程,因为我还有其他应该做的事情。不过在某些时候,我需要确认他们是否已成功下载或上传。
在我目前的代码中,我已经使用了线程库。如果在上传或下载过程中发生错误,处理事务的线程将错误退出。我没有办法通知主线程完成和完成的状态。
我需要做什么才能获得 get_blob
的状态?是否有另一个库可以用一种不那么危险的方式来处理这种情况?我参考了以下线程,但无法弄清楚如何结合它们的不同方法。
Catch a thread's exception in the caller thread in Python
python multiprocessing pool retries
background function in Python
What do I need to do to be able to get the status of
get_blob
?
您可以将 get_blob
包装在一个函数中,该函数将存储有关它是否成功的信息,并存储 return 值(如果有)。您可以写 target=self._get_blob_background
而不是 target=self.blob_service.get_blob_to_path
。新的 _get_blob_background
方法可以调用 self.result = self.blob_service.get_blob_to_path
并使用 try
和 except Exception as e
来捕获所有异常,并在异常情况下执行 self.result_exception = e
,以便主要线程可以区分结果和异常。
更好的是,您可以使用 concurrent.futures
库为您完成所有这些工作:
pool = concurrent.futures.ThreadPoolExecutor()
def get_blob(self):
return pool.submit(self.blob_service.get_blob_to_path,
self.container_name, self.blob_name, self.file_path)
现在 get_blob()
将在后台 运行,就像在您的代码中一样,但这里它将 return 一个 Future
对象,您可以查询该对象以查明是否作业已完成,以及如何完成。