在Python

时间:2017-10-11 21:19:51

标签: python multithreading restart

我编写此脚本是为了从特定ftp上的数据中提取元数据然后将其保存在特定目录中并等待随机时间再次提取等... 所以有2点: 第一:从特定的ftp中提取 第二:保存它并等待随机时间,这取决于使用的ftp。

有时我从ftp有超时问题。当它发生时,没有提取,保存或等待线程了。其他线程继续工作,直到他们遇到超时问题。

我试图捕获异常以便“重新启动”与ftp超时问题有关的线程。但没有任何改变。

有人可以帮我找一个“重启”线程的方法吗?

非常感谢

class ftp_id(Thread):
    def __init__(self, ftpID):
        Thread.__init__(self)
        self.ftpID = ftpID
    def run(self):
        while True:
            with verrou:
                siteID = self.ftpID
                directory = str(datetime.now())[:-16].replace('-', '')
                filename = siteID + '_' + str(datetime.now())[:-7].replace(':', '').replace(' ', '_').replace('-', '') + '.txt'
                dictstr = myExtract(siteID)
                myWriteFile(directory, filename, dictstr)
                pendingtime = myWaiting(siteID, dictstr)
            time.sleep(pendingtime)
        except :
            self.stop = True
            self.stop = False
            self.start()
            self.join()

thread_01 = ftp_id("ftp-01")
thread_02 = ftp_id("ftp-02")
thread_03 = ftp_id("ftp-03")
thread_04 = ftp_id("ftp-04")

thread_01.start()
thread_02.start()
thread_03.start()
thread_04.start()

thread_01.join()
thread_02.join()
thread_03.join()
thread_04.join()

2 个答案:

答案 0 :(得分:2)

线程无法在python中重新启动...粗略的你可以实现自己的“线程”类,而不是真正的线程

class ftp_id(object): # this is a bad name for a class please see pep8
    def __init__(self, ftpID):
        super(ftp_id,self)
        self.ftpID = ftpID
        self.thread = None
        self.thread_stop = False
    def __getattr__(self):
        return getattr(self.thread)
    def start(self):
        if self.thread:
           self.thread_stop = True
           self.thread.terminate()
           self.thread.join()

        self.thread = threading.Thread(target = self.run)
        self.thread.start()
    def run(self):
        self.thread_stop = False
        while not self.thread_stop:
            with verrou:
                siteID = self.ftpID
                directory = str(datetime.now())[:-16].replace('-', '')
                filename = siteID + '_' + str(datetime.now())[:-7].replace(':', '').replace(' ', '_').replace('-', '') + '.txt'
                dictstr = myExtract(siteID)
                myWriteFile(directory, filename, dictstr)
                pendingtime = myWaiting(siteID, dictstr)
            time.sleep(pendingtime)

虽然我不知道这是否真的回答了你的问题

答案 1 :(得分:2)

无法重新启动线程。所以你应该把异常处理放在while - 块中,永远不要退出循环:

def download(server):
    while True:
        try:
            with verrou:
                now = datetime.now()
                directory = '{:%Y%m%d}'.format(now)
                filename = '{}_{:%Y%m%d_%H%M%S}.txt'.format(server, now)
                dictstr = myExtract(server)
                myWriteFile(directory, filename, dictstr)
                pendingtime = myWaiting(server, dictstr)
            time.sleep(pendingtime)
        except:
            # restart
            pass

threads = [
    Thread(target=download, args=(server,))
    for server in ["ftp-01", "ftp-02", "ftp-03", "ftp-04"
]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()