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 2 2272
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |