python线程的导入是什么?

python线程的导入是什么?,python,python-2.7,scrapy,Python,Python 2.7,Scrapy,我想每隔120秒运行一些python代码 我试过这个: class AppServerSvc : def f(self): # call f() again in 120 seconds spider = FantasySerieaSpider() settings = get_project_settings() crawler = Crawler(settings) crawler.signals.co

我想每隔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();
我发现
线程未定义
错误

这是我的作品:

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
而不是(或除了?):

你想要:

import threading

您正在代码中使用
threading.Timer
,但您只从
threading
导入
Thread
,并将其放入当前命名空间。您需要导入整个模块:

import threading
如果使用的是
线程
,请确保将
线程
替换为
线程。线程
。此外,您在一个类中,因此您需要在前缀中添加
self.
,或
f
以引用类成员:

threading.Timer(120, self.f).start()

使用您的代码后,我发现
f
没有定义,因为它是类中的一个函数。我应该说我必须说self.f,我会试试看不,
self.f()
做一个无限循环,请问我该怎么办?@MarcoDinatsoli
self.f
,最后是no
()
。你能检查一下我的新问题吗
threading.Timer(120, self.f).start()