Skip to content

Commit

Permalink
fix: improved event loop
Browse files Browse the repository at this point in the history
It should now support all plugins that use `asyncio` too! :party:
  • Loading branch information
adrienbrignon committed May 30, 2023
1 parent 81db0ab commit b947fe1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 74 deletions.
25 changes: 12 additions & 13 deletions mkdocs_exporter/plugins/pdf/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import types
import asyncio
import nest_asyncio

from mkdocs.plugins import BasePlugin
from mkdocs_exporter.page import Page
Expand All @@ -22,7 +23,14 @@ def __init__(self):
self.watch: list[str] = []
self.renderer: Optional[Renderer] = None
self.tasks: list[types.CoroutineType] = []
self.loop = asyncio.AbstractEventLoop = None
self.loop: asyncio.AbstractEventLoopPolicy = asyncio.new_event_loop()


def on_startup(self, **kwargs) -> None:
"""Invoked when the plugin is starting..."""

nest_asyncio.apply(self.loop)
asyncio.set_event_loop(self.loop)


def on_config(self, config: dict) -> None:
Expand Down Expand Up @@ -69,18 +77,12 @@ def on_page_markdown(self, markdown: str, page: Page, config: Config, **kwargs)
def on_pre_build(self, **kwargs) -> None:
"""Invoked before the build process starts."""

if not self._enabled():
return

self.tasks.clear()

if self.loop and self.loop.is_running():
self.loop.close()
if not self._enabled():
return

self.renderer = Renderer()
self.loop = asyncio.new_event_loop()

asyncio.set_event_loop(self.loop)

for stylesheet in self.config.stylesheets:
self.renderer.add_stylesheet(stylesheet)
Expand Down Expand Up @@ -149,13 +151,10 @@ async def limit(coroutine: Coroutine) -> Coroutine:

self.loop.run_until_complete(asyncio.gather(*concurrently(self.tasks, max(1, self.config.concurrency or 1))))
self.loop.run_until_complete(self.renderer.dispose())
self.loop.close()
self.tasks.clear()

self.loop = None
self.renderer = None

self.tasks.clear()


def _enabled(self, page: Page = None) -> bool:
"""Is the plugin enabled for this page?"""
Expand Down
Loading

0 comments on commit b947fe1

Please # to comment.