简体   繁体   English

在另一个线程中运行在 python3 中的非阻塞套接字服务器

[英]Non-blocking socket server running in python3 in another thread

I would like to know the best way to run a non-blocking python3 socket server.我想知道运行非阻塞 python3 套接字服务器的最佳方法。

I currently have code that is:我目前的代码是:

def start(data):
    global sock
    try:

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(("0.0.0.0", 8080))

        sock.listen(2)

        while True:
            #Does something
            client.close()

    except KeyboardInterrupt:
        kill()

def kill():
    sock.close()

In my main program, how would I make this socket server run in the background (like, in another thread) and not block the main thread so I can continue to do other things in the main thread after the endpoint is created?在我的主程序中,我如何让这个套接字服务器在后台运行(比如,在另一个线程中)而不阻塞主线程,这样我就可以在创建端点后继续在主线程中做其他事情? For example, in my main thread I would like to be able to call createEndpoint(data) and then also call some other functions and etc.例如,在我的主线程中,我希望能够调用createEndpoint(data)然后调用一些其他函数等。

Q : how would I make this … run in the background … and not block the main thread so I can continue…?我如何让这个……在后台运行……而不阻塞主线程,以便我可以继续……?

This will never happen in Python3 as-is (since ever & as Guido ROSSUM declared himself, most probably will remain so, unless total re-engineering of the Python-interpreter will get first decided feasible and next successfully undertaken)这永远不会在 Python3 中按原样发生(自从 Guido ROSSUM 自己宣称以来,很可能会保持这种状态,除非首先确定 Python 解释器的总体重新设计是可行的,然后再成功进行)

Python and all it's threads are centrally controlled by a monopolistic singleton, the central unique shared resource of the Python{2|3} GIL-lock. Python 及其所有线程都由一个垄断的单例集中控制,这是 Python{2|3} GIL 锁的中央唯一共享资源。

The GIL-lock stepping principally re-serialises any just- [CONCURRENT] py-threads-based execution, so the result is a pure- [SERIAL] interleaved sequence ( right - principally avoiding any concurrency at all, the more any wished-to-have form of [PARALLEL] -code execution ). GIL 锁步进主要重新序列化任何基于[CONCURRENT] py-threads 的执行,因此结果是纯[SERIAL]交错序列(正确 -主要避免任何并发,任何希望的越多- 具有[PARALLEL]形式 -代码执行)。

This said, one may jump out of this Python-by-design kept restriction and spawn a non-pythonic framework - like ZeroMQ , that works independently of that, sort of "besides" the python-interpreter, and that would handle all the signalling/messaging services independently over smart sockets (archetyped by agents' behaviours), having fully escaped from the known GIL-lock re-serialisation bottleneck for the performance ( and would also allow to harness the mix of colocated and many-to-many processes' communicating architectures )这就是说,人们可能会跳出这个 Python 设计的限制并产生一个非 Pythonic 框架——比如ZeroMQ ,它独立于它工作,有点“除了” python 解释器,它会处理所有的信号/消息服务独立于智能套接字(以代理行为为原型),完全摆脱了已知的 GIL 锁重新序列化性能瓶颈(并且还允许利用协同定位和的混合多对-许多进程的通信架构)

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

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