简体   繁体   English

不能腌制插座 object python

[英]cannot pickle socket object python

I have a chess game and to check the valid moves which doesn't give a check to the king, I use is_check() method.我有一个国际象棋游戏并检查不给国王检查的有效动作,我使用is_check()方法。 In that, I use deepcopy to copy the board object. It used to work earlier without any exceptions, but recently I connected my chess game my localhost database and it has been giving me cannot pickle socket object error.在那里,我使用 deepcopy 来复制棋盘 object。它以前可以毫无例外地工作,但最近我将我的象棋游戏连接到我的本地主机数据库,它一直给我cannot pickle socket object错误。 Here is my code for the above part.这是我上面部分的代码。

def in_check(self, piece, move):
    # create a deep copy of the piece and the board
    temp_piece = copy.deepcopy(piece)

    temp_board = copy.deepcopy(self)

    # move the piece on the deep copy of the board
    temp_board.move(temp_piece, move)

    # check if any rival pieces can capture the king
    for row in range(TABLE_ROWS):
        for col in range(TABLE_COLUMNS):
            if temp_board.squares[row][col].has_rival(piece.color):
                p = temp_board.squares[row][col].piece

                temp_board.calc_moves(p, row, col, bool=False)
                for m in p.valid_moves:
                    if isinstance(m.final.piece, King):
                        return True
    return False 

this temp_board = copy.deepcopy(self) is the line giving me the error.这个temp_board = copy.deepcopy(self)是给我错误的那一行。 I tried asking ChatGPT but it keeps giving code which tries to exclude socket but it doesn't work.我尝试询问 ChatGPT,但它一直提供试图排除套接字但不起作用的代码。 Anybody got any idea what I can do?有人知道我能做什么吗? greatly appreciated.非常感激。

edit: I think I should also mention that this exception occurs only when I load the game and not when I start a new game.编辑:我想我还应该提到这个异常只在我加载游戏时发生,而不是在我开始新游戏时发生。 It works perfectly when I start a new game but when I load/resume a game.当我开始一个新游戏但当我加载/恢复游戏时它工作得很好。 It gives me this error.它给了我这个错误。

I finally figured out the error.我终于弄清楚了错误。 all of these things happen inside a class Board.所有这些事情都发生在 class Board 中。 when the game is loaded it creates an instance of the SQL connection called self.data and it could not be deepcpoied.加载游戏时,它会创建一个名为self.data的 SQL 连接实例,并且无法对其进行 deepcpoied。 To fix that, I deleted the MySQL object once it loaded the game data from the database.为了解决这个问题,我在从数据库加载游戏数据后删除了 MySQL object。 here is the code,这是代码,

class Board:

   def __init__(self):
    global uid, game_load
    self.black_king_col = None
    self.black_king_row = None
    self.squares = [[0, 0, 0, 0, 0, 0, 0, 0] for _ in range(TABLE_COLUMNS)]
    self.create()  # Calling this function creates the board for the game
    self.last_move = None

    if game_load:
        self.data = SqlData()
        self.data.load_data("white", uid)
        self.data.load_data("black", uid)
        self.load_pieces("white")
        self.load_pieces("black")
        game_load = False
        del self.data   #  This deletes once the above execution is over.

since object is deleted it will not be copied and not cause the error, and also setting the game_load to false because I do not want this block of code to execute again.由于 object 被删除,它不会被复制也不会导致错误,并且还将 game_load 设置为 false 因为我不想再次执行此代码块。

To explain the lines above.解释上面的几行。 I am loading the pieces data from the database into csv files and then fetching that data to my game.我正在将数据库中的片段数据加载到 csv 文件中,然后将该数据提取到我的游戏中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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