访问Python Multiprocessing的状态.父进程的子类



我正在创建一个简单的TCP服务器作为存根,这样我就可以测试操作测试设备的脚本,而不必在那里有设备。服务器应该坐在那里等待连接,然后维护和更新状态变量(只是一个6个整数的列表),以响应它收到的命令。父进程(例如单元测试类)应该能够在任何时候查询状态。

服务器的接口应该像下面这样简单:

server = StubServer()
server.start()
'''
the client script connects with the server and
some stuff happens to change the state
'''
newState = server.getState() # newState = [93,93,93,3,3,45] for example
server.terminate()

我已经子类化了Multiprocessing。这样做的过程,我可以启动服务器没有问题。当我第一次对此进行测试时,在getState()方法中,我只是返回了实例变量_state,但我发现这始终只是初始状态。经过一番挖掘,我找不到任何类似的例子。很多关于子类化过程,但不是这个特定的问题。最后,我把下面的代码放在一起,它使用内部Queue()来存储状态,但这对我来说看起来很混乱和笨拙。有更好的方法吗?

import socket
from multiprocessing import Process, Queue
class StubServer(Process):
    _port = 4001
    _addr = '' # all addresses 0.0.0.0
    _sock = None
    _state = []
    _queue = None
    def __init__(self, initState=[93,93,93,93,93,93]):
        super(StubServer, self).__init__()
        self._queue = Queue()
        self._state = initState
    def run(self):
        # Put the state into the queue
        self._queue.put(self._state)
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.bind((self._addr, self._port))
        self._sock.listen(1)
        waitingForConnection = True
        '''
        Main loop will continue until the connection is terminated. if a connection is closed, the loop returns
        to the start and waits for a new connection. This means multiple tests can be run with the same server
        '''
        while 1:
            # Wait for a connection, or go back and wait for a new message (if a connection already exists)
            if waitingForConnection:
                waitingForConnection = False
                conn, addr = self._sock.accept()
            chunk = ''
            chunks = []
            while 'x03' not in chunk: # 'x03' is terminating character for a message
                chunk = conn.recv(8192)
                if not chunk: # Connection terminated, start the loop again and wait for a new connection
                    waitingForConnection = True
                    break
                chunks.append(chunk)
            message = ''.join(chunks)
            # Now do some stuff to parse the message, and update the state if we received a command
            if isACommand(message):
                _updateState(message)
        conn.close()
        return
    def getState(self):
        # This is called from the parent process, so return the object on the queue
        state = self._queue.get()
        # But put the state back in the queue again so it's there if this method is called again before a state update
        self._queue.put(state)
        return state
    def _updateState(self, message):
        # Do some stuff to figure out what to update then update the state
        self._state[updatedElementIndex] = updatedValue
        # Now empty the queue and put the new state in the queue
        while not self._queue.empty():
            self._queue.get()
        self._queue.put(self._state)
        return

顾名思义,multiprocessing使用不同的进程。在某个时刻,fork()被调用,子进程复制父进程的内存,子进程留下自己的内存,不与父进程共享。

不幸的是,您必须使用可用的工具在进程之间共享内存,从而导致您提到的代码开销。

你可以寻找其他方法来使用共享内存进行并行处理,但是要记住,在线程/进程/节点等之间共享内存从来都不是一件容易的事。

您可以将存根服务器的状态转储到文件中,并随时从unittest中读取它。对于测试需求来说,这是一个非常简单的解决方案。

所有你需要做的:

  • filename作为参数传递给构造器
  • 调用初始值为
  • _updateState
  • 重写_updateState,将状态写入filename。最好在filename附近创建一个新文件并替换它。如果你担心原子性。

谢谢Felipe,我的问题主要是"是否有比使用队列更好的方法",正如我在问题中所做的那样。经过更多的研究(由您提到的共享内存提示),我发现共享数组对于这种情况要好得多:

import socket
from multiprocessing import Process, Array
class StubServer(Process):
    _port = 4001
    _addr = '' # all addresses 0.0.0.0
    _sock = None
    _state = None
    _queue = None
    def __init__(self, initState=[93,93,93,93,93,93]):
        super(StubServer, self).__init__()
        self._state = Array('i', initState) # Is always a 6 element array
    def run(self):
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.bind((self._addr, self._port))
        self._sock.listen(1)
        waitingForConnection = True
        '''
        Main loop will continue until process is terminated. if a connection is closed, the loop returns
        to the start and waits for a new connection. This means multiple tests can be run with the same server
        '''
        while 1:
            # Wait for a connection, or go back and wait for a new message (if a connection already exists)
            if waitingForConnection:
                waitingForConnection = False
                conn, addr = self._sock.accept()
            chunk = ''
            chunks = []
            while 'x03' not in chunk: # 'x03' is terminating character for a message
                chunk = conn.recv(8192)
                if not chunk: # Connection terminated, start the loop again and wait for a new connection
                    waitingForConnection = True
                    break
                chunks.append(chunk)
            message = ''.join(chunks)
            # Now do some stuff to parse the message, and update the state if we received a command
            if isACommand(message):
                _updateState(message)
        conn.close()
        return
    def getState(self):
        # Aquire the lock return the contents of the shared array
        with self._state.get_lock():
            return self._state[:6] # This is OK because we know it is always a 6 element array
        return state
    def _updateState(self, message):
        # Do some stuff to figure out what to update then..
        # Aquire the lock and update the appropriate element in the shared array
        with self._state.get_lock():
            self._state[updatedElementIndex] = updatedValue
        return

这样做效果很好,而且更优雅。谢谢你的帮助

最新更新