I'm new to Python and it seems like I have to write a Script which is able to work parallel. Well, I decided to try to build a multi-threading script which is actual working fine I guess. While doing research I found many examples but only a few worked for me. Finally I read this question (Stackoverflow questions/474528 - second Answer - fifths comment):
The documentation of the shed module also points to the threading.Timer class, which is better suited for multithreaded environments.
For me this sounds like threading.Timer is the better function to use for me in this case. Then I was wondering why ... maybe before I'll go on, on the same link above, the first comment on the second answer, someone is asking
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()?
Now I was thinking about, how can an function be better or not, if I have to Loop and Sleep anyway. I mean the Timer's first parameter only says how long he should wait until start. I tried a little workaround:
#! 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")
This Script is working fine. Now I thought about okay, but why I should use a sleep tho, if the Timer function got the same functionality anyway. So I tried:
#! 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 ( )
But actual I don't get this running correct ... I'm getting this error:
TypeError: print_time() takes 1 positional arguments but 3 were given
I did some research and I found out that the 'self' is the first object so I added the self on the parameters on print_time.
def print_time( self, sThreadName ):
At the beginning I only had the 'sThreadName' as parameter. So the second one is 'sThreadname', but where does a third one comes from?
I did prints for debugging, added a third parameter but don't use it.
def print_time( self, sThreadName, idk ):
#print ( "self: " + self )
#print ( "sThreadName: " + sThreadName )
#print ( "idk: " + idk)
#print ( )
print ( sThreadName + ": " + time.ctime ( time.time ( ) ) )
but then the 'sThreadname' is not correct, it's always like single-letters (e.g. 'n') ... maybe at this point can someone help me out here too?
Besides the fact that the 'sThreadName' is wrong, in case that I'm using 3 parameters on my function and ignore the third one (no usage), my script is not doing what I'm expecting. Im expecting the exactly same behaviour like in my first script posted above. Why this is not the case. I really wanna unterstand this.
So far, thanks in advice for everyone who will try to help me out. Regards, Mike.
Environment: Windows 7 Professional 64-Bit Python 3.4 Code-Editor: Notepad++ Running on CMD by 'python -tt script.py'