我想每120秒运行一些python代码。
我试过了:
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();
我收到threading is not defined
错误
这是我的进口商品:
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
答案 0 :(得分:4)
而不是(或除了?):
from threading import Thread
你想要:
import threading
答案 1 :(得分:4)
您在代码中使用threading.Timer
,但只从Thread
导入threading
并将其放入当前命名空间。你想要的是导入整个模块:
import threading
如果您使用的是Thread
,请务必将Thread
替换为threading.Thread
。此外,您在课堂上,因此您需要在前缀或self.
中添加f
以引用该类成员:
threading.Timer(120, self.f).start()