473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

thread vs threading -- Unexpected Results

CK
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
Jul 18 '05 #1
2 2300
"CK" <fw*****@hotmail.com> wrote in message
news:c0**************************@posting.google.c om...
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))
After the loop completes, the main thread terminates
and so your whole program. You don't wait for the threads
to finish.
Program B - threading example
[...]
for thread in threadlist:
thread.join()

print "Main thread exitting"
And in this program you wait for the threads.
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??


In your threads' code you put the s.close() immediately after s.connect().
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.

Stach

Jul 18 '05 #2
CK
"Krzysztof Stachlewski" <st***@fr.pl> wrote in message news:<bq**********@absinth.dialog.net.pl>...
"CK" <fw*****@hotmail.com> wrote in message
news:c0**************************@posting.google.c om...
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,t hreadId))

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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ola Natvig | last post by:
Anybody out there who knows if the 4suite implementation of XSLT are a threadsafe one? -- -------------------------------------- Ola Natvig <ola.natvig@infosense.no> infoSense AS / development
0
by: Santa | last post by:
I am using Fritz Onion's "Asynchronous Pages" approach as mentioned in the article http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx to increase the performance of my ASPX...
3
by: Keyee Hsu | last post by:
Hi, I have a C# app that creates an AppDomain, enters it, and spawns an asyn thread to do some work and then block itself. Upon the completion of the work, the async thread supposedly terminates,...
1
by: Fred B | last post by:
I am launching a new thread from my application's main process (using VB.net 2003), and I can't get the child to receive the parameter I'm attempting to send it in a named data slot. The code...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
3
by: dedalusenator | last post by:
Hello Folks, My first posting here and I am a stuck in figuring out the exact way to update a global variable from within a function that doesnt return any value (because the function is a...
5
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I am developing a new application. Since it is being developed from scratch, I want to implement a lot of threading - especially for my SQL calls. The problem I noticed is that I can't seem to...
3
by: Mathieu Prevot | last post by:
Hi it seems the script (A) finishes before the downloading ends, and the (B) version doesn't (wanted behavior) ... this is unexpected. What happens ? (A)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.