用map调用类方法

时间:2016-06-23 00:21:31

标签: python multithreading

我在名为`self.plane'的列表中有一个名为Particle的类的实例,我想同时更新。

from multiprocessing import Process, Pool
from timeit import default_timer as current_time

class Particle:
    def __init__(self, X_position, Y_position, X_velocity=0 ,Y_velocity=0, mass=600):
        self.mass = mass
        self.velocity = {'x':X_velocity,'y':Y_velocity}
        self.position = {'x':X_position,'y':Y_position}
        self.time_of_last_update = {'x':current_time(),'y':current_time()}

    def update(self):
        for axis in self.position:
            # Calculate time since update was last run
            elapsed_time = current_time() - self.time_of_last_update[axis]
            # Calculate acceleration
            # acceleration = force[axis] / self.mass
            acceleration = -1
            # Calculate velocity
            self.velocity[axis] = self.velocity[axis] + ( acceleration * elapsed_time )
            # Change position if needed
            if (elapsed_time >= 1 / abs( self.velocity[axis] )):
                # Calculate new postion
                self.position[axis] = self.position[axis] + int( elapsed_time * self.velocity[axis] )
                print(self.position)
                # Reset the timer
                self.time_of_last_update[axis] = current_time()

地图功能似乎是最简单的方法。

我认为我可以让Particle.update()收到self参数,因为self.plane被迭代了。

workers = Pool(processes=4)
workers.map(Particle.update(), self.plane)

但是当我运行时,我得到TypeError: update() missing 1 required positional argument: 'self'

0 个答案:

没有答案