用Python实现Python中的多处理

时间:2016-10-14 14:50:12

标签: python pygame multiprocessing

我试图弄清楚使用pygame进行多处理,但我似乎无法让它发挥作用。我希望有一个窗口显示一些对象,同时使用另一个进程来计算这些对象的某些属性(连续地将这些属性更新到显示窗口)。下面的代码将生成两个窗口(而不是一个)。一个窗口是一个显示窗口,另一个窗口可能是另一个计算对象属性的过程,但似乎这两个窗口没有相互通信。如何让其他进程独立运行(不生成另一个pygame窗口,以便只有显示窗口)?如何在显示窗口中获取对象以从独立进程接收有关其属性的更新?

import sys, pygame
import numpy as np
from multiprocessing import *

class LearningCircle:
    def __init__(self, position, size, name, color, width = 0):
        self.name = name
        self.position = position
        self.size = size
        self.color = color
        self.width = width
    def display(self):#, a):
        #self.size = a[0]
        #self.position = a[1]
        rx, ry = int(self.position[0]), int(self.position[1])
        pygame.draw.circle(screen, self.color, (rx, ry), self.size, self.width)
    def sizer(self):
        if pygame.time.get_ticks() % 1000 == 0:
            self.size = np.random.random_integers(0,20)
            self.position[0] = np.random.random_integers(2,1198)
            self.position[1] = np.random.random_integers(2,798)
    def retriever(self, size, position):
        self.size = size
        self.position = position
LC_list = []
for LC_name in range(0,10):
    LC_list.append(LearningCircle((np.random.random_integers(2,1198), np.random.random_integers(2,798)), name=LC_name, size=40, color=(255, 255, 255), width=0))

screen_size = screen_width, screen_height = 1200, 800
screen = pygame.display.set_mode(screen_size)
clock = pygame.time.Clock()
pygame.display.set_caption('Multiprocessing Test')
fps_limit = 60

def Gamer():
    run_me = True
    pygame.init()
    # Info Display init
    background = pygame.Surface(screen.get_size())
    # Blit everything to the screen
    screen.blit(background, (0, 0))
    while run_me:
        clock.tick(fps_limit)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run_me = False
        screen.unlock()
        Processor()
        for LC in LC_list:
            LC.display()


        pygame.display.flip()
    pygame.quit()
    sys.exit()

def Processor():
    if __name__ == '__main__':
        q = Queue()
        for LC in LC_list:
            p = Process(target=LC.sizer, args=(q,))
            p.start()
            LC.retriever(q.get())
            p.join()
Gamer()

0 个答案:

没有答案