[Zope] Isolating changes that can only be done globally

Chris McDonough chrism at plope.com
Tue May 30 00:21:48 EDT 2006


I suspect you're going to have to subclass the urllib2 Opener class 
(es) and use a timeoutsocket where they create sockets.  Making this  
be an option upstream would be a valuable addition to Python, FWIW.

The other way to do this would be to ditch urllib2 and write a very  
simple HTTP client using asyncore (which is far more malleable).  One  
that I created based on some code I found floating around in RDFLib  
is attached.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: http_client.py
Type: text/x-python-script
Size: 6027 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/zope/attachments/20060530/7058363a/http_client.bin
-------------- next part --------------

On May 29, 2006, at 7:27 PM, Michael Vartanyan wrote:

> Hello All,
>
> This is probably more a Python question but maybe you will have a  
> quick solution for me - I guess the greatest *multi-threaded*  
> Python application provides the greatest basis for this problem  
> domain :-)
>
> The situation: external method that is doing a http request using  
> urllib2. I don't care about the response, and whether there was any  
> response at all, I just need to send the request through - thus I  
> want this request to time out very fast (let it be 2 seconds). I  
> found no documented way to set the timeout for urllib2, after some  
> googling I found an advice to manipulate the timeout on the lower- 
> level socket module, something like this:
>
> import socket
> import urllib2
>
> def do_request():
>  timeout = 2
>  socket.setdefaulttimeout(timeout)
>  req = urllib2.Request(url='http://my.site.com/do_something_quick')
>  response = urllib2.urlopen(req)
>
> The problem is this way this default timeout is set for _all_ new  
> socket created by this Python process using the socket module. Even  
> if I return the default to its previous state after the request it  
> won't help me much - there are three more threads in my Zope that  
> should be able to work with default timeout. So there are two  
> possible solutions - to find a (preferably documented) way of  
> accessing and parameterizing the socket object created by urllib2  
> to make a request or to find a way to isolate(??) global module  
> settings between Zope threads.
>
> Zope 2.8.3, Python 2.4.2, FreeBSD 4.10 if this is relevant.
>
> Any hints/TFMs?
>
> Many thanks
> Michael
>
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )
>



More information about the Zope mailing list