fix: allow running graphs inside an existing event loop (Jupyter, FastAPI) - #1117
Open
hakoke wants to merge 1 commit into
Open
fix: allow running graphs inside an existing event loop (Jupyter, FastAPI)#1117hakoke wants to merge 1 commit into
hakoke wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1116.
Adds
scrapegraphai/utils/event_loop.pywith one helper,run_coroutine_sync: it usesasyncio.run()when no loop is running, and otherwise executes the coroutine on its own loop in a worker thread, since bothasyncio.run()andrun_until_complete()raise inside a running loop. The four crash sites now use it:ChromiumLoader.lazy_load,browser_base_fetch,GraphIteratorNode.executeandGenerateAnswerFromImageNode.execute. The two nodes had the check inverted, callingrun_until_completeprecisely when the loop was running. The worker thread follows the same idea asAbstractGraph.run_safe_async, which already offloads the blocking call to an executor. Plain scripts are unchanged: with no running loop the path is still a directasyncio.run()on the calling thread.Verified on Windows with Python 3.12: a real Playwright fetch through
ChromiumLoader.load()from insideasyncio.run(...)now returns the page instead of raising, same forGraphIteratorNodeandGenerateAnswerFromImageNode(the latter also no longer leaks a cancelled-task traceback), exceptions raised inside coroutines reach the caller unchanged, and the nine test files CI runs pass before and after (66 passed both times). Note on #1087: it refactors the scraping_fn selection a few lines above this change inside lazy_load; if it lands first I will rebase, the overlap is trivial. Happy to add a mocked unit test for the helper and register it in test-suite.yml if you want one.