2014-03-28 129 views
1

我是Python的新手,看起来好像我必须编写能够并行工作的脚本。那么,我决定尝试构建一个多线程脚本,实际工作正常,我猜。在做研究时,我发现了很多例子,但只有少数人为我工作。最后,我读this question (Stackoverflow questions/474528 - second Answer - fifths comment)为什么我应该使用threading.Timer而不是Loop + Sleep?

The documentation of the shed module also points to the threading.Timer class, which is better suited for multithreaded environments.

对我来说,这听起来像threading.Timer是更好的功能,在这种情况下,使用我。然后,我在想为什么...也许之前我会去上,上述同一链路上,在第二个答案的第一个评论,有人问

The sched module is for scheduling functions to run after some time, how do you use it to repeat a function call every x seconds without using time.sleep()?

现在我在想,怎么能一个功能无论如何,如果我必须循环和休眠,那么是否更好。我的意思是定时器的第一个参数只说明他应该等到开始的时间。我尝试了一些解决方法:

#! C:\Python34\env python.exe 
# -*- coding: utf-8 -*- 

import threading 
import time 

class c_Thread (threading.Thread): 
    sThreadName = '' 
    iThreadInterval = 0 

    def __init__ (self, sThreadName, iThreadInterval): 
     threading.Thread.__init__ (self) 
     self.sThreadName = sThreadName 
     self.iThreadInterval = iThreadInterval 
     print () 

    def run (self): 
     print ("Starting " + self.sThreadName) 
     print_time (self.sThreadName, self.iThreadInterval) 
     print ("Exiting " + self.sThreadName) 
     print () 

def print_time(sThreadName, iThreadInterval): 
    while True: 
     time.sleep(iThreadInterval) 
     print (sThreadName + ": " + time.ctime (time.time ())) 

Thread1 = c_Thread ("ThreadOne", 2) 
Thread2 = c_Thread ("ThreadTwo", 5) 

Thread1.start () 
Thread2.start () 

print ("Exiting Main Thread") 

此脚本工作正常。现在我想好了,但为什么我应该使用睡眠寿,如果定时器功能无论如何获得相同的功能。所以,我想:

#! C:\Python34\env python.exe 
# -*- coding: utf-8 -*- 

import threading 
import time 

class c_Thread (threading.Thread): 
    sThreadName = '' 
    iThreadInterval = 0 

    def __init__ (self, sThreadName, iThreadInterval): 
     threading.Thread.__init__ (self) 
     self.sThreadName = sThreadName 
     self.iThreadInterval = iThreadInterval 

    def run (self): 
     print ("Register Timer-Thread: " + self.sThreadName) 
     while True: 
      hTimer = threading.Timer (self.iThreadInterval, print_time, (self.sThreadName)) 
      hTimer . start () 

def print_time(sThreadName): 
    print (sThreadName + ": " + time.ctime (time.time ())) 

Thread1 = c_Thread ("One", 2) 
Thread2 = c_Thread ("Two", 5) 

Thread1 . start () 
Thread2 . start () 

但实际我没有得到这个运行正确的......我得到这个错误:

TypeError: print_time() takes 1 positional arguments but 3 were given

我做了一些研究,我发现了“自我'是第一个对象,所以我在print_time的参数上添加了self。

def print_time(self, sThreadName): 

在开始的时候我只用了“sThreadName”作为参数。所以第二个是'Threadname',但第三个来自哪里?

我做了打印调试,添加了第三个参数,但不使用它。

def print_time(self, sThreadName, idk): 
    #print ("self: " + self) 
    #print ("sThreadName: " + sThreadName) 
    #print ("idk: " + idk) 
    #print () 
    print (sThreadName + ": " + time.ctime (time.time ())) 

但随后的“sThreadname”是不正确的,它总是像单字母(例如“N”)......也许在这一点上有人可以帮助我在这里呢?

除了'sThreadName'错误的情况,如果我在我的函数中使用了3个参数并且忽略了第三个参数(没有用法),我的脚本没有做我期望的。我期待像我在上面发布的第一个脚本完全相同的行为。为什么不是这种情况。我真的想要不要这样。

到目前为止,感谢大家谁会试图帮助我的建议。 问候,迈克。

环境: Windows 7专业版64位 的Python 3.4 代码编辑器:记事本++ 通过 '蟒蛇-tt script.py'

+1

这和C++有什么关系? –

+0

Ohh sry,我编辑了这个,希望删除标签...我没有这么做,谢谢@Joachim Pileborg为了做到这一点 –

回答

0

改变这种运行在CMD:

def run (self): 
    print ("Register Timer-Thread: " + self.sThreadName) 
    while True: 
     hTimer = threading.Timer (self.iThreadInterval, print_time, (self.sThreadName)) 
     hTimer . start () 

太:

def run (self): 
    print ("Register Timer-Thread: " + self.sThreadName) 
    while True: 
     hTimer = threading.Timer (self.iThreadInterval, print_time(self.sThreadName)) 
     hTimer . start () 

所以只是:“print_time,(self.sThreadName)”应该没有com ma“print_time(self。sThreadName)“

+0

Tank先生,这项工作对我来说:-)我还是有一个问题,为什么这两个脚本的行为不一样?你有答案吗? –

+0

你应该检查两个函数的文档,我想我不是那样的在这个主题的专家,所以也许你会明白。[Timer](http://docs.python.org/2/library/threading.html#timer-objects)和[sleep](http://docs.python.org /2/library/time.html#time.sleep)。这应该是你寻找的主要语句“定时器和线程一样,通过调用它们的start()方法来启动。 ...执行动作之前,定时器等待的时间间隔可能与用户指定的时间间隔不完全相同。“ – user3433065

相关问题