Solve Python Scrapy Error Reactor not Restartable

Solve Python Scrapy Error Reactor not Restartable is an error which occurs in Python because You can’t start a twisted reactor more than once.

In this article we are going to explain why the error is popping up and show you how to solve the error and get rid of it for good.

Explaining the Python Scrapy Error Reactor not Restartable

The error can happen to anyone who tries to start a twisted reactor more than once in a code using Scrapy.

here is an example of the error message.

                                                                       #
Traceback (most recent call last):
File "main.py", line 573, in <module>
print (main())
File "main.py", line 530, in __call__
    
.
.
.
  File "/Library/Python/2.7/site-packages/twisted/internet/milly.py", line 684, in startRunning
    raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : A Step by Step Fix

First of all, you need to understand that this error happens because you are running the reactor more than once in the same script.

However, we can fix the issue. Please make sure to follow these instructions Step by Step.

First, start by running the command bellow.

                                                                       #
pip install crochet
                                                                       #

Then

                                                                       #
import from crochet import setup
                                                                       #

At the top of the script/file you are working with, add setup() to the the top.

Make sure, you remove reactor.run() from your code. Also make sure to remove d.addBoth(lambda _: reactor.stop())

This should be enough to solve your issue, if it does not achieve the goal, please try the solution bellow.

Solution 2 : use time.sleep

The second solution is to use time.sleep like in the code bellow, just add the two lines to your code.

                                                                       #
time.sleep(0.5)
os.execl(sys.executable, sys.executable, *sys.argv)
                                                                       #

Make sure you have added the two lines after process.start() or reactor.run().

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

This is the end of our guide, I hope you found at least one solution useful, we try to provide solutions for both Linux and windows users, If you did not solve the issue ‘Python Scrapy Error Reactor not Restartable’ , I hope you find it online, do not give up, errors are normal in Python, Cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/