[Tutor] threading issues

Kent Johnson kent37 at tds.net
Mon Oct 24 00:55:54 CEST 2005


Chris Hallman wrote:
> 
> I made a mistake in my first email..... I meant that I can't get fc to 
> write to the file. Here is the error:
> 
> Traceback (most recent call last):
>   File "thread_test_ping.py", line 37, in ?
>     output.write(fc + " failures found.\n")
> TypeError: unsupported operand type(s) for +: 'int' and 'str'

This is a pretty easy error to understand. It is saying that you can't add an int and a string. Looking at your code, you are trying to add fc + " failures found.\n". fc is indeed an int. You have to change it to a string to be able to add it to another string. You can do this with the str() function. So the correct statement is
  output.write(str(fc) + " failures found.\n")

> 
> I tried the suggestions you made, but I can't get it to work. Is this 
> what you meant?:

No, not quite.
> 
> #!/usr/bin/env python
> 
> #Let us profile code which uses threads
> import os, re, string, thread, threading, time
> from time import strftime
> #from threading import Thread
> 
> class PingThread(threading.Thread):
>     def __init__(self, rtr):
>         threading.Thread.__init__(self)
>         self.rtr = rtr

The init method initializes a PingThread. It is passed self as an argument, plus whatever parameters you pass in when you create the PingThread. You call the superclass constructor and save the value of the parameter. So far so good...but this next line is creating a PingThread, it belongs below in your loop.

>         PingThread(rtr).start(rtr)
>    
>     def run(self):

Here you should reference self.rtr so you get the value that was stored in the call to __init__(), and similarly for each reference to rtr in the run method.

>         pingaf = os.popen('ping -n 1 -w 3 ' + rtr)
>         pingas = string.join(pingaf.readlines())
>         if ms.search(pingas):
>             print (re.sub('\n','',rtr)) + " responded."    #for debugging
>         else:
>             pingaf = os.popen('ping -n 2 -w 3 ' + rtr)
>             pingas = string.join(pingaf.readlines())
>             if ms.search(pingas):
>                 print (re.sub('\n','',rtr)) + " responded."    # for 
> debugging
>             else:
>                 fc=fc+1
>                 output.write(re.sub('\n','',rtr) + " did not respond.\n")
> 
> fc = 0    # failure counter
> ms = re.compile("Reply from")
> rpath = (r"c:\temp\py\network_ping_routers.txt")
> if os.path.exists(r"c:\temp\py\network_ping_again.txt"):
>     rpath = (r"c:\temp\py\network_ping_again.txt")
> wpath = (r"c:\temp\py\network_ping.out")
> #os.system(r"c:\tnd\bin\cawto.exe -cat NetNet -n l17aesm1 forward blue 
> held Weekly ISDN testing has started -" + strftime(" %H:%M:%S %x") + "\n")
> output = open(wpath, "a")
> output.write("\n" + "\n" + "Network PING test started -" + strftime(" 
> %H:%M:%S %x") + "\n")
> output.flush()
> threads = []
> for rtr in file(rpath):
>     thread = PingThread()
Here is where you need to call PingThread(rtr) to pass the value of rtr to the constructor.

>     thread.start()
>     threads.append(thread)
> for thread in threads:
>     thread.join()
> print fc    # for debugging
> output.write(fc + " failures found.\n")
> output.write("\n" + "\n" + "Network PING test completed -" + strftime(" 
> %H:%M:%S %x") + "\n")
> output.close()

I suspect that I am talking over your head and that maybe you are in over your head a bit here. If you don't understand the basics of classes and parameter passing you should spend some time with a tutorial that explains them, for example Alan Gauld's tutorial at
http://www.freenetpages.co.uk/hp/alan.gauld/

There is a brief introduction to threading here. It even uses your problem (pinging multiple servers) as the example.
http://www.wellho.net/solutions/python-python-threads-a-first-example.html

Kent
http://www.kentsjohnson.com



More information about the Tutor mailing list