Advertisement
furas

Python - Pygame + multiprocessing (example: SO)

Oct 14th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. #
  2. # http://stackoverflow.com/questions/40046088/implementing-multiprocessing-in-python-with-python?noredirect=1#
  3. #
  4.  
  5. import sys
  6. import pygame
  7. import numpy as np
  8. from multiprocessing import *
  9.  
  10. # --- constants --- (UPPER_CASE names)
  11.  
  12. SCREEN_WIDTH = 1200
  13. SCREEN_HEIGHT = 800
  14. SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
  15.  
  16. FTP_LIMIT = 60
  17.  
  18. # --- classes --- (CamelCase names)
  19.  
  20. class LearningCircle:
  21.    
  22.     def __init__(self, position, size, name, color, width=0):
  23.         self.position = position
  24.         self.size = size
  25.         self.name = name
  26.         self.color = color
  27.         self.width = width
  28.        
  29.     def display(self, screen):
  30.         pygame.draw.circle(screen, self.color, self.position, self.size, self.width)
  31.  
  32.     def update(self, size, position):
  33.         self.size = size
  34.         self.position = position
  35.        
  36.     def sizer(self, q, seed):
  37.         # `get_ticks()` doesn't return every number
  38.         # so `get_ticks() % 1000` can't run function every 1000ms
  39.  
  40.         # if get_ticks() - prevous_ticks >= 1000:
  41.        
  42.         # for this moment I use `% 10` but it need better solution
  43.         if pygame.time.get_ticks() % 10 == 0:
  44.             # seed need some random value
  45.             # without `seed(random_value)` every random_integers in every Proccess generate the same values
  46.             np.random.seed(seed)
  47.            
  48.             size = np.random.random_integers(20,40)
  49.             x = np.random.random_integers(2, SCREEN_WIDTH-2)
  50.             y = np.random.random_integers(2, SCREEN_HEIGHT-2)
  51.             q.put( (size, (x, y)) )
  52.         #else:
  53.         #    q.put( None )
  54.  
  55. # --- functions --- (lower_case names)
  56.  
  57. def processor(LC_list):
  58.     q = Queue()
  59.  
  60.     for i, LC in enumerate(LC_list):
  61.         # every Procces needs random seed
  62.         # otherwise all Processes generate the same values
  63.         seed = np.random.random_integers(0, 10000)
  64.        
  65.         p = Process(target=LC.sizer, args=(q, seed))
  66.         p.start()
  67.        
  68.         #result = q.get()
  69.         #if result is not None:
  70.         #    print(result)
  71.         #    LC.update(result[0], result[1])
  72.  
  73.         if not q.empty():
  74.             result = q.get() # don't wait for result
  75.             print(result)
  76.             LC.update(result[0], result[1])
  77.            
  78.         p.join()
  79.            
  80. # --- main --- (function lower_case names)
  81.  
  82. def gamer():
  83.    
  84.     LC_list = []
  85.    
  86.     for LC_name in range(10):
  87.         LC_list.append(
  88.             LearningCircle(
  89.                 (np.random.random_integers(2, SCREEN_WIDTH-2),
  90.                  np.random.random_integers(2, SCREEN_HEIGHT-2)),
  91.                  name=LC_name, size=40, color=(255, 255, 255), width=0
  92.             )
  93.         )
  94.        
  95.     pygame.init()
  96.  
  97.     screen = pygame.display.set_mode(SCREEN_SIZE)
  98.     pygame.display.set_caption('Multiprocessing Test')
  99.     clock = pygame.time.Clock()
  100.    
  101.     # Info Display init
  102.     background = pygame.Surface(screen.get_size())
  103.    
  104.     run_me = True
  105.     # Blit everything to the screen
  106.     while run_me:
  107.         clock.tick(FTP_LIMIT)
  108.  
  109.         for event in pygame.event.get():
  110.             if event.type == pygame.QUIT:
  111.                 run_me = False
  112.  
  113.         screen.unlock()
  114.        
  115.         # maybe starts Processes before `while` loop
  116.         # and here receives only data from queues
  117.         processor(LC_list)
  118.  
  119.         screen.blit(background, (0, 0))
  120.         for LC in LC_list:
  121.             LC.display(screen)
  122.         pygame.display.flip()
  123.        
  124.     pygame.quit()
  125.     sys.exit()
  126.    
  127. if __name__ == '__main__':
  128.     gamer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement