Skip to content

Conversation

yuji38kwmt
Copy link
Collaborator

@yuji38kwmt yuji38kwmt commented Aug 12, 2024

変更前

以下のように、asyncio.get_event_loop()でイベントループを取得して、asyncio.gather()で並行に実行していました。

    def wait_until_job_is_executable(...):
        ...
        gather = asyncio.gather(
            *[
                self._wait_until_job_finished_async(project_id, new_job_type, None, job_access_interval, max_job_access)
                for new_job_type in job_type_list
            ],
            return_exceptions=True,
        )
        loop = asyncio.get_event_loop()
        result = loop.run_until_complete(gather)

しかし、wait_until_job_is_executableをコルーチンで実行すると、RuntimeErrorが発生しました。

sample.py

import asyncio
import annofabapi
from annofabapi.models import ProjectJobType
s = annofabapi.build()

async def main():
   s.wrapper.wait_until_job_is_executable("58a2a621-7d4b-41e7-927b-cdc570c1114a", ProjectJobType.GEN_INPUTS)
   
asyncio.run(main())
$ python sample.py
Traceback (most recent call last):
  File "/home/yuji/workspace/github.com/annofab-api-python-client/out/sample.py", line 15, in <module>
    asyncio.run(main())
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/yuji/workspace/github.com/annofab-api-python-client/out/sample.py", line 13, in main
    s.wrapper.wait_until_job_is_executable("58a2a621-7d4b-41e7-927b-cdc570c1114a", ProjectJobType.GEN_INPUTS)
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/site-packages/annofabapi/wrapper.py", line 2315, in wait_until_job_is_executable
    result = loop.run_until_complete(gather)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/asyncio/base_events.py", line 663, in run_until_complete
    self._check_running()
  File "/home/yuji/.pyenv/versions/3.12.4/lib/python3.12/asyncio/base_events.py", line 622, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

変更後

asyncio.gatherで並行実行したときと、逐次的に実行したときで、待ち時間は変わりません。
したがって、asyncioを使わずに逐次的な処理に変更しました。
そうすることで、コルーチンでもwait_until_job_is_executableを呼び出すことができるようになります。

@yuji38kwmt yuji38kwmt changed the title 非同期実行から同期実行に変更する Wrapper.can_execute_jobto非同期実行から同期実行に変更する Aug 12, 2024
@yuji38kwmt yuji38kwmt changed the title Wrapper.can_execute_jobto非同期実行から同期実行に変更する Wrapper.can_execute_job, Wrapper.wait_until_job_is_executableの処理を非同期実行か同期実行に変更しました。 Aug 12, 2024
@yuji38kwmt yuji38kwmt enabled auto-merge (squash) August 12, 2024 05:06
@yuji38kwmt yuji38kwmt merged commit 177baf7 into main Aug 12, 2024
@yuji38kwmt yuji38kwmt deleted the update-async branch August 12, 2024 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant