简体   繁体   中英

Python multiprocessing ForkingPickler issue

I'm working a socket client well really 2 clients, one sends messages and the other receives messages. There are two socket server, one is listening for commands and the other is providing responses. I'm trying to write a double client, which allows me to send messages and will spin up another process to receive messages from response server. They aren't always synchronous. I create this little class that I'd like it to spin up a process to listen responses, but I'm stuck with this error when I run it:

ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle module objects

The little program is as follows:

import socket
import logging
import time
from multiprocessing import Pool, Process, Manager, Pipe
import traceback
import re


class Geisy3:
    received_messages = []
    dbg_process = None

    def __init__(self):
        print('hello')

    def connect(self, ip, port):
        s = socket.socket()
        logging.basicConfig(filename="geisy-3.log", level=logging.INFO,
                            format='%(asctime)s %(levelname)s %(message)s')
        logging.info('Icci debug IP: %s, cmd port: %s', ip, port)
        s.connect((ip, port))
        logging.info('Socket connection established on %s:%s', ip, port)

        with Manager() as manager:
            self.received_messages = manager.list()
            self.dbg_process = Process(target=self.socket_receiver, args=(self, s, self.received_messages, logging))
            self.dbg_process.start()
            self.dbg_process.join()

        print('Debug process started')

    def socket_receiver(self, soc, received_messages, logging):
        try:
            while 1:
                logging.debug('Socket listener is awaiting message')
                raw_response = soc.recv(256)
                logging.debug('RAW Msg recv [%s]: %s', 20001, raw_response)


        except Exception as e:
            logging.FATAL('Exception %s:', e)
            logging.FATAL('Debug listner exception occured %s', traceback.format_exc())


if __name__ == '__main__':
    i = Geisy3()

    i.connect('98.1.24.40', 20001)
    time.sleep(500)

I can't figure out why I'm getting this error. Is it because windows spawns rather than forks?

Can I actually have a method in the class that starts, joins and then effectively another that kills the process?

There are 4 parts (I only control 2 on my end). I'm getting this on my end while trying to setup a listener. This code works as soon as I take it outside of the class. I'm having trouble wrapping my head around on how this should be implemented inside a class. The sender from end we can actually ignore completely (it works fine and its another class). The sever to which the above listener connects sends heartbeat messages, so I should be able to have the process running and have it print those messages.

Like I said as standalone script without class it works great, jut the problem is that I need to embedded that within another program hence I thought of putting it inside a class to have little more control from that other program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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