简体   繁体   English

mulithread或multiprocess中的python socket.connect超时错误

[英]python socket.connect timeout error in mulithread or multiprocess

Like the following, I'd like to communicate with many PC's in a specific IP range. 如下所示,我想与特定IP范围内的许多PC进行通信。

My PC ---+------> Client A PC
         +------> Client B PC
         +------> Client C PC
         .................
         +------> Client Z PC

Because there are too many clients to communicate, I tried it by mulit-threading. 因为有太多客户端要沟通,所以我尝试了多线程。 socket.connect() continuously produces time-out error. socket.connect()不断产生超时错误。 If I try it in a single-thread, there's no problem. 如果我在单线程中尝试它,那就没问题了。

I googled and found the below : 我用Google搜索并找到了以下内容:

Python Interpreter blocks Multithreaded DNS requests? Python解释器阻止多线程DNS请求?

saying that in some platform, socket module could be thread unsafe. 说在某些平台上,套接字模块可能是线程不安全的。

So I changed my code into multi-processing. 所以我将代码更改为多处理。 However it still produces the same error. 但是它仍然会产生相同的错误。

In the following code sample, test_single() finishes normal. 在以下代码示例中,test_single()完成正常。 test_mp() and test_mt() both make time-out error. test_mp()和test_mt()都会导致超时错误。

Have you ever experienced such abnormal behavior? 你有没有经历过这种异常行为? The testing environment is Windows XP SP3, python 2.5.4. 测试环境是Windows XP SP3,python 2.5.4。 Also tried on python 2.6.6 and 2.7.0, same error. 还尝试了python 2.6.6和2.7.0,同样的错误。

import multiprocessing
import Queue
import socket
import threading

PROCESS_NUM = 5
PORT = 8888

def search_proc(ip):
    try:
        csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        csock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        csock.settimeout(5.0)
        csock.connect((ip, PORT))
        csock.shutdown(socket.SHUT_RDWR)
        csock.close()
        return ip, "ok"
    except socket.error, msg:
        return ip, "fail", msg

def mp_connect(ip_range):
    pool = multiprocessing.Pool( PROCESS_NUM )
    for output in pool.imap_unordered(search_proc, ip_range):
        print output

def test_mp():
    ip_range = []
    for i in range(256):
        ip_range.append("192.168.123.%d"%(i,))

    mp_connect(ip_range)

def test_mt():
    def search_thread(ip_queue):
        while True:
            ip = ip_queue.get()
            print search_proc(ip)
            ip_queue.task_done()
    ip_queue = Queue.Queue()

    for i in range(256):
        ip_queue.put("192.168.123.%d"%(i,))

    for i in range(PROCESS_NUM):
        th = threading.Thread(target=search_thread, args=(ip_queue,))
        th.setDaemon(True)
        th.start()

    ip_queue.join()

def test_single():
    ip_range = []
    for i in range(256):
        print search_proc("192.168.123.%d"%(i,))

if __name__ == "__main__":
    multiprocessing.freeze_support()
    test_mp()
    #test_single()
    #test_mt()

David Beazley has done some great research around the Python GIL and how that affects IO and multithreading. David Beazley围绕Python GIL做了一些很好的研究,以及它如何影响IO和多线程。 You can find information about his research here , here . 您可以 此处找到有关其研究的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM