Open ThinksFast opened 1 year ago
Just to clarify, I'm not suggesting to remove self._asyncioEventloop.run_forever()
to resolve the issue. Only that my observation was commenting it out, in my MRE, did not seem to cause any obvious side effects.
I'm not sure what the right solution is here, but the two I can think of is:
.run_forever()
on a loop thats already runningrun_forever()
(is that the same as loop.is_running()
?)It sounds like you are trying to load Twisted in to an already-running mainloop, start Twisted's reactor asynchronously, and have it continue running.
I think that the thing you want to call is reactor.startRunning()
which is like a … partially-official API for doing this, but it should mostly work. There's an open question about how to test this so that we can support it more fully.
Describe the incorrect behavior you saw I am running
Scrapy
inside an ASGI framework calledLitestar
and usingUvicorn
as the server.Scrapy
is built on top ofTwisted
.When I run a
scrapy
crawler from insideLitestar
, I get the following exception:I verified
scrapy
is correctly passingtwisted
the same instance of the event loop created byUvicorn
. In this case, it'suvloop
, although I tried the standardasyncio
loop withuvicorn
and got the same result.The line of code causing this exception is
self._asyncioEventloop.run_forever()
, in theAsyncioSelectorReactor
run()
function. Commenting it out kills the exception and the scrapy crawl runs without issue.There is also an issue on shutdown, but I haven't investigated this yet. Here's what the logs look like on shutdown:
Describe how to cause this behavior
What did you do to get it to happen? Running a scrapy crawl (using Twisted) from inside an ASGI framework.
Does it happen every time you follow these steps, sometimes, or only one time? Happens every every time. Here is the full traceback.
How to Reproduce A complete MRE is located here: https://github.com/ThinksFast/litestar-scrapy/tree/main
Describe the correct behavior you'd like to see There should not be an exception thrown when Twisted is passed an existing loop. Shutdown should also be graceful.
Testing environment
Additional context Add any other context about the problem here.