簡體   English   中英

Python 多處理 ForkingPickler 問題

[英]Python multiprocessing ForkingPickler issue

我正在使用一個套接字客戶端,實際上有 2 個客戶端,一個發送消息,另一個接收消息。 有兩個套接字服務器,一個是偵聽命令,另一個是提供響應。 我正在嘗試編寫一個雙客戶端,它允許我發送消息並將啟動另一個進程以從響應服務器接收消息。 它們並不總是同步的。 我創建了這個小 class 我希望它啟動一個進程來監聽響應,但是當我運行它時我遇到了這個錯誤:

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

小程序如下:

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)

我不知道為什么我會收到這個錯誤。 是因為 windows 產生而不是分叉嗎?

我真的可以在 class 中有一個方法來啟動,加入然后有效地殺死進程嗎?

有 4 個部分(我只控制 2 個)。 我在嘗試設置偵聽器時遇到了這個問題。 一旦我把它帶到 class 之外,這個代碼就會起作用。 我很難理解如何在 class 中實現這一點。 我們實際上可以完全忽略最后的發件人(它工作正常,它是另一個類)。 上述偵聽器連接的服務器發送心跳消息,所以我應該能夠讓進程運行並讓它打印這些消息。

就像我所說的沒有 class 的獨立腳本它工作得很好,但問題是我需要將它嵌入到另一個程序中,因此我想把它放在 class 中,以便從其他程序獲得更多控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM