Socket connection

Jp Calderone exarkun at intarweb.us
Thu Mar 6 22:16:03 EST 2003


On Fri, Mar 07, 2003 at 01:10:34AM +0000, Donn Cave wrote:
> Quoth Jp Calderone <exarkun at intarweb.us>:
> | On Thu, Mar 06, 2003 at 03:32:15PM -0600, sik0fewl wrote:
> |> 
> |> [snip]
> |> 
> |> Like I said, the select() works fine when connecting to localhost, but 
> |> when I try any other address (like www.hotmail.com) the select keeps 
> |> timing out and x never becomes connected.
> |
> |   You've made the socket non-blocking.  This means socket.connect() will
> | return right away, *even if it has not yet connected*.  Even leaving this
> | aside, your code has a race condition, nothing says that the connection in
> | the thread will be made before the select() loop begins.
> 
> I think that's as it should be, and the select is supposed to deal
> with it.  But since he also ignores exceptions from the connect()
> attempt, he turns every possible error into a silent select timeout.
> 

  Good point.

> |   I'd recommend -either- using threads -or- using non-blocking sockets, not
> | both (I'd further recommend not using threads).
> |
> |   There are libraries available to make this sort of programming easier. 
> | You might check out Twisted - http://www.twistedmatrix.com/
> 
> It sure seems to be the rage this week.  Do you know if it has the
> asynchronous DNS lookup he'll need, to replace the threads?
> 

  Indeed it does, though not by default (yet).  To enabled async lookups,
this is required at some point:

    from twisted.internet import reactor
    from twisted.names import client
    reactor.installResolver(client.theResolver)

  Jp

-- 
 up 3 days, 19:58, 5 users, load average: 0.02, 0.01, 0.00





More information about the Python-list mailing list