gpt4 book ai didi

python - Django和Socket服务器: Can I add socket server instance to “memory” ?

转载 作者:行者123 更新时间:2023-12-03 11:55:06 25 4
gpt4 key购买 nike

我正在与:

  • Django 1.11
  • Python套接字

  • 我有一个这样的套接字服务器:
    class SocketServer(threading.Thread):

    def __init__(self, ip="127.0.0.1", port=5000, _buffer=1024):
    super(RoomSocketServer, self).__init__()
    self.IP = ip
    self.PORT = port
    self.RECV_BUFFER = _buffer # Advisable to keep it as an exponent of 2
    self.CONNECTION_LIST = [] # list of socket clients
    self.MESSAGE_QUEUES = {} # List of message queue by socket
    self.OUTPUTS = []
    self.SERVER = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # this has no effect, why ?
    self.SERVER.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.SERVER.bind((self.IP, self.PORT))
    self.SERVER.listen(10)

    # Add server socket to the list of readable connections
    self.CONNECTION_LIST.append(self.SERVER)
    self.ROOM = Room.objects.create(port=port, ip=ip, type_room=type_room)

    def read_sockets(self, read_sockets):
    ''' ... '''

    def write_sockets(self, write_sockets):
    ''' ... '''

    def error_sockets(self, error_sockets):
    ''' ... '''

    def run(self):
    while 1:
    # Get the list sockets which are ready to be read through select
    read_sockets, write_sockets, error_sockets = select.select(self.CONNECTION_LIST, self.OUTPUTS, [])

    # Read sockets
    self.read_sockets(read_sockets)
    self.write_sockets(write_sockets)
    self.error_sockets(error_sockets)

    self.SERVER.close()

    我可以在Django的任何地方运行这样的SocketServer(custom_command, View , celery ...):
    from socket_server import SocketServer

    socket_server = SocketServer()
    socket_server.start()
    # And the code continues while the socket server is running
    # I would like to save socket_server instance anywhere to access
    # Later from anywhere or trigger a signal to finish it

    就像我在上面说的那样,我想知道(如果可能的话)你们中的任何人将服务器实例保存到Django项目的不同部分来访问它吗?

    更新

    我尝试在Django上使用memcached,但是当我尝试将SocketServer实例存储在memcached上时,出现此错误:

    PicklingError: Can't pickle : attribute lookup thread.lock failed

    最佳答案

    答案是肯定的,而且很简单

    serverlst = dict()
    for x in range(0,5):
    tmp = SocketServer("",5000+x) # Note that you should change the port because each port can be listened only by one socket server
    tmp.start()
    serverlst[5000+x]=tmp
    或者您可以使用以下命令访问服务器套接字
    serverlst[5000].getName()   # returns the server socket with port 5000's thread
    循环后打印 serverlst时,您可以看到此信息
    {5000: <SocketServer(Thread-5, started 139741574940416)>, 5001: <SocketServer(Thread-6, started 139741583333120)>, 5002: <SocketServer(Thread-7, started 139741658834688)>, 5003: <SocketServer(Thread-8, started 139741667227392)>, 5004: <SocketServer(Thread-9, started 139741233895168)>}
    或者您可以将其添加到 list()更新
    对不起,我没看到

    I would like to know (if possible) where would any of you save the instance of the server to access it from different parts of the Django project


    我可以说这取决于您的发展方式,我认为这不是一个好主意。最好在线程和Django项目(例如 redis)之间使用桥接器,该桥接器允许您保留数据并从整个系统访问数据,并且可以使用 python redis client

    关于python - Django和Socket服务器: Can I add socket server instance to “memory” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618721/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com