我正在尝试开发具有以下要求的系统部分:
我正在使用socketio.AsyncClient来满足这些要求。
class CompileJobHandler(socketio.AsyncClientNamespace):
def __init__(self, namespace_val):
super().__init__(namespace_val)
// some init variables
async def _clone_git_repo(self, git_repo: str):
// clone repo and return its instance
return repo
async def on_availability_check(self, data):
// the health status
await self.emit('availability_check', " all good ")
async def on_cancel_job(self, data):
// cancel the current job
def _reset_job(self):
// reset job logics
def _reset_to_specific_commit(self, repo: git.Repo, commit_hash: str):
// reset to specific commit
def _compile(self, is_debug):
// compile logics - might be CPU intensive
async def on_execute_job(self, data):
// **request to execute the job(compile in our case)**
try:
repo = self._clone_git_repo(job_details.git_repo)
self._reset_to_specific_commit(repo, job_details.commit_hash)
self._compile(job_details.is_debug)
await self.emit('execute_job_response',
self._prepare_response("SUCCESS", "compile successfully"))
except Exception as e:
await self.emit('execute_job_response',
self._prepare_response(e.args[0], e.args[1]))
finally:
await self._reset_job()
以下代码的问题是,当execute_job消息到达时,正在运行的阻塞代码会阻塞整个async-io系统。
为解决此问题,我使用了ProcessPoolExecutor
和asyncio事件循环,如下所示:https://stackoverflow.com/questions/49978320/asyncio-run-in-executor-using-processpoolexecutor
使用它之后,克隆/编译功能将在另一个进程中执行-几乎可以实现我的目标。
我的问题是:
一种方法是保持这种状态,另一种方法是预先初始化一个对象(我们称其为CompileExecuter
并创建此类型的实例,并在开始该过程之前对其进行预初始化,然后让该过程使用它)
如何在执行过程中停止该进程?(如果收到on_cancel_job
请求)
如何正确处理流程引发的异常?
欢迎使用其他满足这些要求的方法