流程之间的数据共享问题 [英] Issues with data sharing between processes

查看:114
本文介绍了流程之间的数据共享问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,正在研究一个多处理示例脚本.该代码长约100行,因此您可以在这里找到它- http://pastie.org/1813365

I'm rather new to Python and I'm working on a multiprocessing example script. The code is ~100 lines long, so you can find it here - http://pastie.org/1813365

这是一个发生在正方形字段上的简单游戏-它们以矩阵表示(列表包含x个嵌套的列表,每个嵌套x个元素).为每个玩家创建一个单独的游戏字段.

It's a simple game that occurs on square fields - they are represented as matrices(list containing x nested lists of x elements each). A separate game field is created for each player.

我创建的每个过程都代表一个玩家.每回合,对于每个玩家",将随机生成两个坐标和一个数字(0-9).每个玩家"都尝试将其数字放置在他在该字段中的坐标上;如果这些坐标中其他玩家的字段上没有数字,或者该数字小于第一个玩家的数字,或者如果第一个玩家的数字为"0"(就像小丑卡"),则该数字为(当然,应该是)放置在两个字段上,并且玩家的得分会增加.游戏在指定的迭代次数后结束.

Each process that I create represents one player. Each turn and for each "player", two coordinates and a digit(0-9) are generated(randomly). Each "player" tries to place its' digit on his coordinates in the field; if there is no digit on another players' field in these coordinates, or the digit is less than the first players' one, or if the first player has the '0'(it is like a 'joker card') - the digit is (well, meant to be) placed on both fields and the player's score is increased. The game ends after a specified number of iterations.

所有数据都存储在包含数据的类的单个对象中,该对象正从主线程传输到第一个播放器"线程,然后不断地从一个线程来回传输到另一个线程,直到游戏结束.为此使用"JoinableQueue".

All the data is stored in a single object of the data-containing class that is being transferred from the main thread to the "first player" thread and then is constantly being transferred from one thread to another, back and forth, until the game ends. 'JoinableQueue' is being used for that.

有关代码的问题是,似乎每个玩家"都有自己的两个游戏领域的副本.如果您每回合都从两个玩家输出两个游戏场,则可以清楚地看到这一点-它们对于每个玩家都是相同的.例如,注释行无效,因为永远不会修改另一位玩家(在回合中正在打印的)字段:

The problem about the code is that it seems that each "player" has its' own copies of both of the game fields. You can see this clearly if you output both game fields from both player each turn - they are identical for each player. For example, the commented line has no effect, since the another player's (that is being printed in his turn) field is never modified:

if x.data_PC[y2][x2] == 'X' or z2 == 0 or int(x.data_PC[y2][x2]) < z2:         
    x.data_PC2[y2][x2] = str(z2) # doesn't work
    x.data_PC[y2][x2] = str(z2)
    x.score_PC2 += 1

这特别奇怪,因为对象中的所有剩余数据似乎都可以正常工作.

This is especially strange since all the remaining data in the object seem to be working just fine.

是什么原因导致这种行为?我该如何解决?

What causes such a behavior and how can I fix this?

推荐答案

好的,您的问题与多处理无关.如果查看您的data类定义(您应该将类​​名大写,那是 pythonic ),那么您创建的所有变量都是类变量.您希望它们在__init__方法中并定义为self.foo=1,以便它们是实例变量,并且可以在进程之间传递.像这样:

Ok, your problem doesn't have anything to do with multiprocessing. If you look at your data class definition(you should capitalize your class names, it's pythonic that way), all the variables you are creating are class variables. You want them to be in an __init__ method and defined as self.foo=1 so that they are instance variables and can be passed between the processes. Like so:

class Data(object):

    def __init__(self):

        self.SIZE = 8 # dimensions of the game field
        self.MAX_TURNS = 5 # amount of iterations for each of the players ("turns") before the end
        self.turns = 0 # count of turns done
        self.score_PC = self.score_PC2 = 0 # score values

        # create and init the data matrices
        self.data_PC = [['X' for j in xrange(self.SIZE)] for i in xrange(self.SIZE)]
        self.data_PC2 = [['X' for j in xrange(self.SIZE)] for i in xrange(self.SIZE)]

现在多谈谈多处理方面.如果您正在使用可连接队列,则实际上并不需要锁.您需要做的只是.get()数据,对其进行处理,然后再将其.put()放回到队列中,并让它知道.task_done().它将负责并发.

Now a few words about the multiprocessing aspect. You don't really need a lock if you are using the joinable queue. All you need to do is .get() the data, process it and .put() it back in the queue, and let it know that .task_done(). It will take care of the concurrency.

在这里,我发布了您的代码以及所需的修改.

这篇关于流程之间的数据共享问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆