簡體   English   中英

python什么是線程導入?

[英]python what is the import for threading?

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

而不是(或除了?):

from threading import Thread

你要:

import threading

您正在使用threading.Timer在你的代碼,但你僅導入Threadthreading ,並把它變成了當前的命名空間。 您想要的是導入整個模塊:

import threading

如果使用的是Thread ,請確保使用threading.Thread替換Thread 另外,您正在上課,因此您需要添加self. 在前綴或f中引用類成員:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM