我怎样才能非阻塞套接字连接()的? [英] How can I get non-blocking socket connect()'s?

查看:130
本文介绍了我怎样才能非阻塞套接字连接()的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个很简单的问题。我需要大量的主机同时通信,但我并不真的需要任何同步,因为每个请求是pretty自给自足。

I have a quite simple problem here. I need to communicate with a lot of hosts simultaneously, but I do not really need any synchronization because each request is pretty self sufficient.

由于这个原因,我选择了与异步套接字工作,而不是垃圾邮件线程。
现在我有一个小问题:

Because of that, I chose to work with asynchronous sockets, rather than spamming threads. Now I do have a little problem:

异步的东西用得好好的,但是当我连接到100台主机,我也得到100超时(超时= 10秒),那么我等待千秒,只是为了找出我的所有连接失败。

The async stuff works like a charm, but when I connect to 100 hosts, and I get 100 timeouts (timeout = 10 secs) then I wait 1000 seconds, just to find out all my connections failed.

有什么办法也得到非阻塞插座连接?
我的插座已被设置为非阻塞,但调用连接()仍然阻塞。

Is there any way to also get non blocking socket connects? My socket is already set to nonBlocking, but calls to connect() are still blocking.

减少超时不是一个可接受的解决方案。

Reducing the timeout is not an acceptable solution.

我在Python这样做,但我猜编程语言犯规在这种情况下,真正的问题。

I am doing this in Python, but I guess the programming language doesnt really matter in this case.

我是否真的需要使用线程?

Do I really need to use threads?

推荐答案

您需要并行化所连接为好,因为当你设置一个超时的套接字阻塞。或者,你可以不设置超时,并使用选择的模块。

You need to parallelize the connects as well, since the sockets block when you set a timeout. Alternatively, you could not set a timeout, and use the select module.

您可以将 asyncore 模块与调度类做到这一点。看看基本的 HTTP客户端的例子。这个类的多个实例不会阻止上相互连接。你可以很容易地做到这一点使用线程,我认为品牌跟踪套接字超时更容易,但既然你已经在使用异步方法你还不如留在同一轨道上。

You can do this with the dispatcher class in the asyncore module. Take a look at the basic http client example. Multiple instances of that class won't block each other on connect. You can do this just as easily using threads, and I think makes tracking socket timeouts easier, but since you're already using asynchronous methods you might as well stay on the same track.

作为一个例子,在我所有的Linux系统下的作品

As an example, the following works on all my linux systems

import asyncore, socket

class client(asyncore.dispatcher):
    def __init__(self, host):
        self.host = host
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, 22))

    def handle_connect(self):
        print 'Connected to', self.host

    def handle_close(self):
        self.close()

    def handle_write(self):
        self.send('')

    def handle_read(self):
        print ' ', self.recv(1024)

clients = []
for i in range(50, 100):
    clients.append(client('cluster%d' % i))

asyncore.loop()

凡在cluster50 - cluster100,有无数的机器是反应迟钝,或不存在。这立即开始打印:

Where in cluster50 - cluster100, there are numerous machines that are unresponsive, or nonexistent. This immediately starts printing:

Connected to cluster50
  SSH-2.0-OpenSSH_4.3

Connected to cluster51
  SSH-2.0-OpenSSH_4.3

Connected to cluster52
  SSH-2.0-OpenSSH_4.3

Connected to cluster60
  SSH-2.0-OpenSSH_4.3

Connected to cluster61
  SSH-2.0-OpenSSH_4.3

...

然而,这并不考虑的getaddrinfo,它具有阻止。如果您在解决DNS查询问题,一切都必须等待。你可能需要单独收集自己的DNS查询,并在异步循环使用的IP地址

This however does not take into account getaddrinfo, which has to block. If you're having issues resolving the dns queries, everything has to wait. You probably need to gather the dns queries separately on your own, and use the ip addresses in your async loop

如果你想比asyncore一个更大的工具包,看看扭曲的矩阵。这是一个有点重进入,但它是最好的网络编程工具包,你可以得到蟒蛇。

If you want a bigger toolkit than asyncore, take a look at Twisted Matrix. It's a bit heavy to get into, but it is the best network programming toolkit you can get for python.

这篇关于我怎样才能非阻塞套接字连接()的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆