rather than (or in addition to?):
from threading import Thread
you want:
import threading
Вопрос
I want to run some python code every 120 seconds.
I tried this:
class AppServerSvc :
def f(self):
# call f() again in 120 seconds
spider = FantasySerieaSpider()
settings = get_project_settings()
crawler = Crawler(settings)
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run() # the script will block here until the spider_closed signal was sent
threading.Timer(120, f).start()
if __name__ == '__main__':
AppServerSvc().f();
I got the threading is not defined
error
This are my imports:
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy import log, signals
from FantasySeriea.spiders.spider import FantasySerieaSpider
from scrapy.utils.project import get_project_settings
from threading import Thread
Решение
rather than (or in addition to?):
from threading import Thread
you want:
import threading
Другие советы
You're using threading.Timer
in your code but you're importing only Thread
from threading
and putting it into the current namespace. What you want is to import the whole module:
import threading
If you are using Thread
, make sure to replace Thread
by threading.Thread
. Also, you are in a class, so you need to add self.
in prefix or f
to refer the class member:
threading.Timer(120, self.f).start()