-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easy to break installing the custom reactor #4401
Comments
I am working on this issue. |
True, this is what happened to me in scrapinghub/testspiders#10, I didn't realize there were still conflicting imports within Scrapy itself. |
@wRAR I have tried a lot to get "wrong reactor installed" error but cannot get it. Can you provide an example? |
@adityaa30 set |
@wRAR I have found out that, even if don't do I feel this is because from __future__ import division, absolute_import
import sys
del sys.modules['twisted.internet.reactor']
from twisted.internet import default
default.install() I want to know If I am going in correct direction? |
Then it looks like some other code imports |
@wRAR Okay thanks! I think I have a good idea now what the exact issue is. I will soon send a pr fixing this issue. Also, shall I add in documentation that the user code shouldn't do similar top-level imports in the same pr? If yes, where in docs should this be added? |
The docs should say what can happen, why, and how to prevent it. It's also not just about user code but also about 3rd part libraries that are imported in user code. I think a good place for this is in https://docs.scrapy.org/en/latest/topics/settings.html#std:setting-TWISTED_REACTOR |
Okay, working on it. |
Should/could we perhaps wrap and replace the |
@nyov I think this is possible. A custom class which takes a library/package name as string with customized attribute accessing. We can override For example: import importlib
class LazyLoader:
def __init__(self, lib_name):
self.lib_name = lib_name
self._mod = None
def __getattr__(self, name):
if self._mod is None:
self._mod = importlib.import_module(self.lib_name)
return getattr(self._mod, name) Now, reactor = LazyLoader('twisted.internet.reactor') Upd: @wRAR this may be a good idea as we can declare |
I created a spider that imports
scrapy.mail
and got "wrong reactor installed" after settingTWISTED_REACTOR
. This is becausescrapy.mail
importstwisted.internet.reactor
at the top level and becausescrapy crawl
imports all spider modules.So I think we should kill all top-level
twisted.internet.reactor
imports (it was partially done when adding the feature, so that importingCrawlerProcess
doesn't install the reactor) and add documentation that the user code shouldn't do similar top-level imports.The text was updated successfully, but these errors were encountered: