thread vs threading -- Unexpected Results

CK fw58959 at hotmail.com
Fri Dec 5 10:04:46 EST 2003


"Krzysztof Stachlewski" <stach at fr.pl> wrote in message news:<bqn9no$qp6$1 at absinth.dialog.net.pl>...
> "CK" <fw58959 at hotmail.com> wrote in message
> news:c0367340.0312032212.70ab984a at posting.google.com...
> 
> > Program A - thread example
> >
     .... snip ...
> 
> After the loop completes, the main thread terminates
> and so your whole program. You don't wait for the threads
> to finish.
> 

Good point.  I added code to check for this (a la Programming
Python by Mark Lutz) but the results are really strange. All I see are
Connecting messages and never a disconnecting message and 
so now the whole program hangs waiting for these "hung"
threads to wake up.

Here is the revised "thread" code

#!/usr/local/bin/python

import socket
import time
import thread

threadlist = [0] * 2048
threadId = -1
def tcp_connect(dst_ip,dst_port,thisId):
  print "%s Connecting to %s on port %d  ThreadId=%d" % (time.asctime(),dst_ip,d
st_port,thisId)
  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  try:
    s.connect((dst_ip,dst_port))
  except:
    pass
  s.close()
  threadlist[thisId] = 1
  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):
         threadId = threadId + 1
         ip_addr = "10.34."+str(octet3)+"."+str(octet4)
         thread.start_new_thread(tcp_connect,(ip_addr,135,threadId))

while 0 in threadlist:
  time.sleep(2)

print "Main thread exitting"

and here is some sample output....

Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.0 on port 135  ThreadId=0
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.5 on port 135  ThreadId=5
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.6 on port 135  ThreadId=6
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.7 on port 135  ThreadId=7
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.8 on port 135  ThreadId=8
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.9 on port 135  ThreadId=9
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.10 on port 135  ThreadId=10
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.11 on port 135  ThreadId=11
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.12 on port 135  ThreadId=12
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.13 on port 135  ThreadId=13
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.14 on port 135  ThreadId=14
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.15 on port 135  ThreadId=15
Fri Dec  5 06:54:12 2003 Connecting to 10.34.32.16 on port 135  ThreadId=16
...

Never see a Disconnecting message, never see "Main thread exitting"



> > Program B - threading example
....snip....
> The connections are closed very quickly. This time is so short
> that the scheduler did not switched tasks.
> Why you see a different pattern of calls I cannot tell.
> Probably an artefact of the task scheduler?
> I would not worry about this. All of your
> operations completed within one second.
> Try to put something - at least a delay - between
> connect() and close() and see how it works.

You are correct on this one.  I added a time.sleep(2) just
before the close and I got results that one would expect.

I don't know what I am doing wrong in the 'thread' example
but the 'threading' example works just fine.

Thank you for your help Stach.

./CK




More information about the Python-list mailing list