Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # http://stackoverflow.com/questions/40046088/implementing-multiprocessing-in-python-with-python?noredirect=1#
- #
- import sys
- import pygame
- import numpy as np
- from multiprocessing import *
- # --- constants --- (UPPER_CASE names)
- SCREEN_WIDTH = 1200
- SCREEN_HEIGHT = 800
- SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
- FTP_LIMIT = 60
- # --- classes --- (CamelCase names)
- class LearningCircle:
- def __init__(self, position, size, name, color, width=0):
- self.position = position
- self.size = size
- self.name = name
- self.color = color
- self.width = width
- def display(self, screen):
- pygame.draw.circle(screen, self.color, self.position, self.size, self.width)
- def update(self, size, position):
- self.size = size
- self.position = position
- def sizer(self, q, seed):
- # `get_ticks()` doesn't return every number
- # so `get_ticks() % 1000` can't run function every 1000ms
- # if get_ticks() - prevous_ticks >= 1000:
- # for this moment I use `% 10` but it need better solution
- if pygame.time.get_ticks() % 10 == 0:
- # seed need some random value
- # without `seed(random_value)` every random_integers in every Proccess generate the same values
- np.random.seed(seed)
- size = np.random.random_integers(20,40)
- x = np.random.random_integers(2, SCREEN_WIDTH-2)
- y = np.random.random_integers(2, SCREEN_HEIGHT-2)
- q.put( (size, (x, y)) )
- #else:
- # q.put( None )
- # --- functions --- (lower_case names)
- def processor(LC_list):
- q = Queue()
- for i, LC in enumerate(LC_list):
- # every Procces needs random seed
- # otherwise all Processes generate the same values
- seed = np.random.random_integers(0, 10000)
- p = Process(target=LC.sizer, args=(q, seed))
- p.start()
- #result = q.get()
- #if result is not None:
- # print(result)
- # LC.update(result[0], result[1])
- if not q.empty():
- result = q.get() # don't wait for result
- print(result)
- LC.update(result[0], result[1])
- p.join()
- # --- main --- (function lower_case names)
- def gamer():
- LC_list = []
- for LC_name in range(10):
- LC_list.append(
- LearningCircle(
- (np.random.random_integers(2, SCREEN_WIDTH-2),
- np.random.random_integers(2, SCREEN_HEIGHT-2)),
- name=LC_name, size=40, color=(255, 255, 255), width=0
- )
- )
- pygame.init()
- screen = pygame.display.set_mode(SCREEN_SIZE)
- pygame.display.set_caption('Multiprocessing Test')
- clock = pygame.time.Clock()
- # Info Display init
- background = pygame.Surface(screen.get_size())
- run_me = True
- # Blit everything to the screen
- while run_me:
- clock.tick(FTP_LIMIT)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run_me = False
- screen.unlock()
- # maybe starts Processes before `while` loop
- # and here receives only data from queues
- processor(LC_list)
- screen.blit(background, (0, 0))
- for LC in LC_list:
- LC.display(screen)
- pygame.display.flip()
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- gamer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement