thread vs threading -- Unexpected Results

CK fw58959 at hotmail.com
Thu Dec 4 01:12:00 EST 2003


I am a "newbie" to python and today I had the need to 
write a program which generated a lot of tcp connections
to a range of addresses (10.34.32.0/22) in order to
troubleshoot a problem with a switch.  I also wanted
to get familiar with threads under python and so I 
thought I could do both at the same time.  I wrote
two programs - one using thread and one using threading
but the results between the two were unexpected and quite
different.  I was hoping that someone could shed some
light on why there is such a difference.

Program A - thread example

#!/usr/local/bin/python

import socket
import time
import thread

def tcp_connect(dst_ip,dst_port):
  print "%s Connecting to %s on port %d" %
(time.asctime(),dst_ip,dst_port)
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  try:
    s.connect((dst_ip,dst_port))
  except:
    pass
  s.close()
  print "%s Disconnecting from %s on port %d" %
(time.asctime(),dst_ip,dst_port)

for x in range(0,2):
  for octet3 in range(32,36):
      for octet4 in range(0,256):
         ip_addr = "10.34."+str(octet3)+"."+str(octet4)
         thread.start_new_thread(tcp_connect,(ip_addr,135))

produces the following output

pus-bin[49]% ./tcp_connector.py
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.0 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.5 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.10 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.15 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.20 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.25 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.24 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.23 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.22 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.21 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.19 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.18 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.17 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.16 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.14 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.13 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.12 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.11 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.9 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.8 on port 135
Wed Dec  3 21:36:01 2003 Connecting to 10.34.32.7 on port 135
...etc...

At most I see only 2 Disconnecting prints.  I would have
expected to see Disconnect prints intermingled in the output.
In fact I don't see any Disconnecting prints (other than the two).   
Why is this?

And now for something completely different....:-)

Program B - threading example

#!/usr/local/bin/python

import socket
import time
import threading

class connector(threading.Thread):
  def __init__(self,dst_ip,dst_port):
    self.dst_ip = dst_ip
    self.dst_port = dst_port
    threading.Thread.__init__(self)
  def run(self):
    print "%s Connecting to %s on port %d" %
(time.asctime(),self.dst_ip,self.dst_port)
    self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
      self.s.connect((dst_ip,dst_port))
    except:
      pass
    self.s.close()
    print "%s Disconnecting from %s on port %d" %
(time.asctime(),self.dst_ip,self.dst_port)

threadlist = []
for x in range(0,4):
  for octet3 in range(32,36):
      for octet4 in range(0,256):
         ip_addr = "10.34."+str(octet3)+"."+str(octet4)
         thread = connector(ip_addr,135)
         thread.start()
         threadlist.append(thread)

for thread in threadlist:
   thread.join()

print "Main thread exitting"

which produces the following output...

Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.0 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.0 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.1 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.1 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.2 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.2 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.3 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.3 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.4 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.4 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.5 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.5 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.6 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.6 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.7 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.7 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.8 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.8 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.9 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.9 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.10 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.10 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.11 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.11 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.12 on port 135
Wed Dec  3 21:56:42 2003 Disconnecting from 10.34.32.12 on port 135
Wed Dec  3 21:56:42 2003 Connecting to 10.34.32.13 on port 135
.... etc ...

Now in the threading example I have Connect / Disconnect pairs for the
whole cycle.  I would have expected multiple Connects here and there
and multiple Disconnects appearing here and there but it looks like
at most only two threads are ever active (main + one).  Why don't
I see multiple Connects / Disconnects??

Thanks.
./CK




More information about the Python-list mailing list