向正在运行的python进程发送命令

向正在运行的python进程发送命令,python,python-3.x,tcp,tcpserver,Python,Python 3.x,Tcp,Tcpserver,我想用TCP服务器从服务器到客户端进行通信。关于这一点,我的问题是,当有人从web调用python脚本时,我会做什么(即,什么是常用方法)从不同的线程向客户端发送字节。有什么方法可以做到这一点吗?您可以为连接客户端时创建的线程定义一个成员变量。然后使用锁写入此变量。此变量将由所有线程共享: import threading class ConnectedClients(threading.Thread): used_ressources = list() used_resso

我想用TCP服务器从服务器到客户端进行通信。关于这一点,我的问题是,当有人从web调用python脚本时,我会做什么(即,什么是常用方法)从不同的线程向客户端发送字节。有什么方法可以做到这一点吗?

您可以为连接客户端时创建的线程定义一个成员变量。然后使用锁写入此变量。此变量将由所有线程共享:

import threading

class ConnectedClients(threading.Thread):

    used_ressources = list()
    used_ressources_lock = threading.Lock()

    def run(self, ressource_to_get):
        if ressource_to_get in used_ressources:
            raise Exception('Already used ressource:' + repr(ressource_to_get))
        else:
            can_access = self.used_ressources_lock.acquire(blocking=True, timeout=5)
            if can_access:
                self.used_ressources.append(ressource_to_get)
                self.used_ressources_lock.release()
                # Do something...
                # You will have to acquire lock and remove ressource from
                # the list when you're done with it.
            else:
                raise Exception("Cannot acquire lock")

您正在寻找这样的产品吗?

您能更具体地说明您的需求吗?如果您的客户端使用浏览器连接到服务器,则您可能希望使用。如果您已经使用线程开发了一些代码,那么请提供一些示例,以便我们可以帮助您。我不知道如何才能更具体,再次解释:我想用python开发TCP服务器(这非常简单)。此服务器响应。它运行的线程应该被告知其他进程,例如,有人使用http在服务器上调用不同的脚本。是的,没错,非常感谢!无论如何,我现在将对服务器使用Objective-C,因为它更可靠,更易于控制,有更多的库,服务器和客户端使用相同的语言,等等。