473,378 Members | 1,470 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,378 software developers and data experts.

scope of socket.setdefaulttimeout?

Does anyone know the scope of the socket.setdefaulttimeout call? Is it
a cross-process/system setting or does it stay local in the application
in which it is called?

I've been testing this and it seems to stay in the application scope,
but the paranoid side of me thinks I may be missing something... any
confirmation would be helpful.

Sep 29 '05 #1
5 4196
Russell Warren wrote:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it
a cross-process/system setting or does it stay local in the application
in which it is called?

I've been testing this and it seems to stay in the application scope,
but the paranoid side of me thinks I may be missing something... any
confirmation would be helpful.

Yes, it's an application setting, you aren't changing things for anyone
else.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Sep 30 '05 #2
It appears that the timeout setting is contained within a process
(thanks for the confirmation), but I've realized that the function
doesn't play friendly with threads. If I have multiple threads using
sockets and one (or more) is using timeouts, one thread affects the
other and you get unpredictable behavior sometimes. I included a short
script at the end of this that demonstrates the threading problem.

I'm trying to get around this by forcing all locations that want to set
a timeout to use a 'safe' call immediately prior to socket creation
that locks out setting the timeout again until the lock is released.
Something like this:

try:
SafeSetSocketTimeout(Timeout_s)
#lock currently acquired to prevent other threads sneaking in here
CreateSocket()
finally:
ReleaseSocketTimeoutSettingLock()
UseSocket()

However - this is getting increasingly painful because I don't have
easy access to all of the socket creations where I'd like to do this.
The biggest pain right now is that I'm using xmlrpclib which has some
seriously/frustratingly heavy use of __ prefixes that makes getting
inside to do this at socket creation near impossible (at least I think
so). Right now the best I can do is surround the xmlrpclib calls with
this (effectively putting the lock release after the UseSocket), but
then other threads get hung up for the duration of the call or timeout,
rather than just the simple socket creation.

It would be nice if the timeout were implemented as an argument in the
socket constructor rather than having this global method. Is there a
reason for this? I tried sifting through the cvs source and got lost -
couldn't even find the call definition for socket(family, type, proto)
and gave up...

Does anybody have any idea of another way to do what I need (indpendent
socket timeouts per thread), or have suggestions on how to break into
xmlrpclib (actually down into httplib) to do the methdo I was trying?

Related question: Is there some global way that I'm unaware of to make
it so that some few lines of code are atomic/uninterruptable and no
other thread can sneak in between?

All suggestions appreciated! Hopefully I'm just missing something
obvious.

Russ

#--- This script confirms that settimeout's affect is across threads
import threading, xmlrpclib, socket

def st():
socket.setdefaulttimeout(0.1)

try:
proxy = xmlrpclib.ServerProxy("http://localhost:10000")
print proxy.NonExistentCallThatShouldTimeout()
except Exception, E: print "Exception caught: %s" % (E,)

cbThread = threading.Thread(target = st)
cbThread.start()

try:
print proxy.NonExistentCallThatShouldTimeout()
except Exception, E: print "Exception caught: %s" % (E,)

#Output is:
#Exception caught: (10061, 'Connection refused')
#Exception caught: timed out

Oct 12 '05 #3
Russell Warren wrote:
It appears that the timeout setting is contained within a process
(thanks for the confirmation), but I've realized that the function
doesn't play friendly with threads. If I have multiple threads using
sockets and one (or more) is using timeouts, one thread affects the
other and you get unpredictable behavior sometimes. I included a short
script at the end of this that demonstrates the threading problem.
When you say "one thread affects another", I see that your example uses
the same function for both threads. IMHO it's much better to override
the thread's run() method than to provide a callable at thread creating
time. That way you can be sure each thread's execution is firmly in the
context of the particular thread instance's namespace.

having said all this, I don't think that's your issue.
I'm trying to get around this by forcing all locations that want to set
a timeout to use a 'safe' call immediately prior to socket creation
that locks out setting the timeout again until the lock is released.
Something like this:

try:
SafeSetSocketTimeout(Timeout_s)
#lock currently acquired to prevent other threads sneaking in here
CreateSocket()
finally:
ReleaseSocketTimeoutSettingLock()
UseSocket()
This seems extremely contorted, and I'm pretty sure we can find a better
way.
However - this is getting increasingly painful because I don't have
easy access to all of the socket creations where I'd like to do this.
The biggest pain right now is that I'm using xmlrpclib which has some
seriously/frustratingly heavy use of __ prefixes that makes getting
inside to do this at socket creation near impossible (at least I think
so). Right now the best I can do is surround the xmlrpclib calls with
this (effectively putting the lock release after the UseSocket), but
then other threads get hung up for the duration of the call or timeout,
rather than just the simple socket creation.
The threads' network calls should be yielding process control during
their timeout period to allow other runnable threads to proceed. That's
how it's always worked for me, anyway, and I've written an email sender
with 200 parallel threads.
It would be nice if the timeout were implemented as an argument in the
socket constructor rather than having this global method. Is there a
reason for this? I tried sifting through the cvs source and got lost -
couldn't even find the call definition for socket(family, type, proto)
and gave up...
You are aware, I presume, that you can set a timeout on each socket
individually using its settimeout() method?
Does anybody have any idea of another way to do what I need (indpendent
socket timeouts per thread), or have suggestions on how to break into
xmlrpclib (actually down into httplib) to do the methdo I was trying?
See above. However, this *does* require you to have access to the
sockets, which is tricky if they are buried deep in some opaque object's
methods.
Related question: Is there some global way that I'm unaware of to make
it so that some few lines of code are atomic/uninterruptable and no
other thread can sneak in between?
There are locks! I suspect what you need is a threading.Rlock object,
that a thread has to hold to be able to modify the (global) default
timeout. This isn't a full solution to your problem, though, as you have
correctly deduced.

The problem arises when a single method call creates a socket and then
tries to do something with it (like connect to a remote server), which
*is* problematic. I have to say that I haven't ever used different
timeout values for the sockets in different parallel threads, so this is
a new problem to me.
All suggestions appreciated! Hopefully I'm just missing something
obvious.

Russ

#--- This script confirms that settimeout's affect is across threads
import threading, xmlrpclib, socket

def st():
socket.setdefaulttimeout(0.1)

try:
proxy = xmlrpclib.ServerProxy("http://localhost:10000")
print proxy.NonExistentCallThatShouldTimeout()
except Exception, E: print "Exception caught: %s" % (E,)

cbThread = threading.Thread(target = st)
cbThread.start()

try:
print proxy.NonExistentCallThatShouldTimeout()
except Exception, E: print "Exception caught: %s" % (E,)

#Output is:
#Exception caught: (10061, 'Connection refused')
#Exception caught: timed out

Here (unless I'm missing something obvious) it seems that your worker
thread terminates immediately after setting the default timeout, and
both of the proxy calls are made from the main thread, so I'm not
particularly surprised at the results, given the global nature of the
default socket timeout.

Maybe someone else can think of something that will help.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 12 '05 #4
Thanks for the detailed repsone... sorry for the lag in responding to
it.

After reading and further thought, the only reason I was using
setdefaulttimeout in the first place (rather then using a direct
settimeout on the socket) was because it seemed like the only way (and
easy) of getting access to the seemingly deeply buried socket being
used by xmlrpclib. That was prior to me using threads of course. I
then started trying to make this solution work with thread, but it is
now too convoluted as you say. Now I think the best solution is likely
to redirect my efforts at getting access to the socket used by
xmlrpclib so that I can set it's timeout directly. I'm still unclear
how to do this cleanly, though.

Getting to some of your comments.
When you say "one thread affects another", I see that your example uses
the same function for both threads. IMHO it's much better to override
the thread's run() method than to provide a callable at thread creating
time. That way you can be sure each thread's execution is firmly in the
context of the particular thread instance's namespace.

having said all this, I don't think that's your issue.
Correct - the bottom code is nothing to do with my code and was only to
quickly prove that it was cross-thread.
This seems extremely contorted, and I'm pretty sure we can find a better
way.
Couldn't agree more!
The threads' network calls should be yielding process control during
their timeout period to allow other runnable threads to proceed. That's
Yep. This is not causing me any problem.
You are aware, I presume, that you can set a timeout on each socket
individually using its settimeout() method?
Yes, but I momentarily had forgot about it... as mentioned I ended up
making the since-bad choice of using setdefaulttimeout to get timeouts
set on the inaccessible sockets. Then I carried it too far...
See above. However, this *does* require you to have access to the
sockets, which is tricky if they are buried deep in some opaque object's
methods.
Any help on how to crack the safe would be appreciated.
There are locks! I suspect what you need is a threading.Rlock object,
that a thread has to hold to be able to modify the (global) default
timeout. This isn't a full solution to your problem, though, as you have
correctly deduced.


Not quite what I was after I don't think since potentially interfering
code needs to check the lock (via acquire) to avoid conflict. What I
guess I mean is something general for the process saying "never ever
interrupt this block og code by running code on another thread,
regardless of whether the other thread(s) check a lock". Thinking more
about it it seems unreasonable so I'll drop the question.

Russ

Oct 19 '05 #5
Russell Warren wrote:
Thanks for the detailed repsone... sorry for the lag in responding to
it. [discussion of problems with timeouts on threaded code's sockets]
Not quite what I was after I don't think since potentially interfering
code needs to check the lock (via acquire) to avoid conflict. What I
guess I mean is something general for the process saying "never ever
interrupt this block og code by running code on another thread,
regardless of whether the other thread(s) check a lock". Thinking more
about it it seems unreasonable so I'll drop the question.


Well, I'm about out of ideas, but c.l.py is a very inventive group, so
maybe someone else will be able to contribute a bright thought. Anyone?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 19 '05 #6

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

Similar topics

1
by: Fortepianissimo | last post by:
I've tried using socket.setdefaulttimeout(timeout) to set the default timeout to 'timeout' for all sockets. The part that's not clear to me, is that if this will affect...
0
by: Tim Williams | last post by:
(python newbie) Is it possible to individually set the socket timeout of a connection created by smtplib, rather than use the socket.setdefaulttimeout() value used by the rest of the...
1
by: John Hunter | last post by:
I have a test script below which I use to fetch urls into strings, either over https or http. When over https, I use m2crypto.urllib and when over http I use the standard urllib. Whenever, I...
10
by: Sheila King | last post by:
I'm doing DNS lookups on common spam blacklists (such as SpamCop..and others) in an email filtering script. Sometimes, because the DNS server that is resolving the looksup can go down, it is...
1
by: rtilley | last post by:
Perhaps this is a dumb question... but here goes. Should a socket client and a socket server each have different values for socket.setdefaulttimeout() what happens? Does the one with the shortest...
0
by: Jaap Spies | last post by:
Hi, Running Fedora Core 4: Python 2.4.3 and Python 2.4.1. I'm getting: IOError: (2, 'No such file or directory') all the time. Trying to track down this problem: Python 2.4.1 (#1, May 16...
1
by: John Nagle | last post by:
Does setting "socket.setdefaulttimeout" affect the timeout in MySQLdb for connections to the database? I'm getting database connection timeouts on a local (same machine) connnection, and I've been...
2
by: Robin Becker | last post by:
While messing about with some deliberate socket timeout code I got an unexpected timeout after 20 seconds when my code was doing socket.setdefaulttimeout(120). Closer inspection revealed that...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.