【问题标题】:python what is the import for threading?python 线程的导入是什么?
【发布时间】:2014-05-09 20:29:17
【问题描述】:

我想每 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

【问题讨论】:

    标签: python python-2.7 scrapy


    【解决方案1】:

    而不是(或除了?):

    from threading import Thread
    

    你想要的:

    import threading
    

    【讨论】:

    • 使用您的代码后,我得到 f 未定义,认为它是类内的函数。我应该我不得不说self.f,会试试的
    • 不,self.f() 出现无限循环,请问我该怎么办?
    • @MarcoDinatsoli self.f,最后没有()
    • 你能检查我的新问题吗stackoverflow.com/questions/24109713/…
    【解决方案2】:

    您在代码中使用了threading.Timer,但您仅从threading 导入Thread 并将其放入当前命名空间。你想要的是导入整个模块:

    import threading
    

    如果您使用的是Thread,请确保将Thread 替换为threading.Thread。另外,你在一个班级,所以你需要在前缀中添加self.f来引用班级成员:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2013-02-11
      • 2010-09-12
      相关资源
      最近更新 更多