Advertisement
here2share

# Tk_parallel_pixel_multiprocessing2.py

Aug 10th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # Tk_parallel_pixel_multiprocessing2.py -- fastest in Python said to bypass GIL, especially when compiled... but maybe still much slower than a C variant.
  2.  
  3. from Tkinter import *
  4.  
  5. import time
  6. import random
  7. from PIL import Image, ImageTk
  8.  
  9. from multiprocessing import Pool, cpu_count
  10.  
  11. root = Tk()
  12. root.title("Y-Axis-Parallel_Pixels")
  13. root.geometry("120x120")
  14. wi = 120
  15. he = 120
  16. w = Canvas(root, width=wi, height=he)
  17. w.pack()
  18.  
  19. def thread_process(y):
  20.     return [(0,random.randrange(y*2+15, 255),0) for x in range(wi)]
  21.  
  22. if __name__ == '__main__':
  23.     pool = Pool(processes=cpu_count())
  24.     img = Image.new( 'RGB', (wi,he))
  25.     while 1:
  26.         data = []
  27.         for y in range(he):
  28.             p = pool.map_async(thread_process, (y,))
  29.             data.extend(p.get()[0])
  30.         img.putdata(data)
  31.         imgTk = ImageTk.PhotoImage(img)
  32.        
  33.         ### saw no glitches
  34.         #time.sleep(0.02)
  35.        
  36.         w.create_image(0, 0, anchor=NW, image=imgTk)
  37.         root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement