在asyncio应用程序中执行CPU绑定操作的最优雅方法?

时间:2019-10-25 09:26:43

标签: python-multiprocessing python-asyncio flask-socketio conan

我正在尝试开发具有以下要求的系统部分:

  1. 每隔X秒将健康状态发送到远程服务器
  2. 接收执行/取消CPU绑定作业的请求(例如-克隆git repo,对其进行编译(使用conan等等)。

我正在使用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

使用它之后,克隆/编译功能将在另一个进程中执行-几乎可以实现我的目标。

我的问题是:

  1. 如何更好地设计流程代码?(现在我有一些静态函数,我不喜欢它...)

一种方法是保持这种状态,另一种方法是预先初始化一个对象(我们称其为CompileExecuter并创建此类型的实例,并在开始该过程之前对其进行预初始化,然后让该过程使用它)

  1. 如何在执行过程中停止该进程?(如果收到on_cancel_job请求)

  2. 如何正确处理流程引发的异常?

欢迎使用其他满足这些要求的方法

0 个答案:

没有答案
相关问题