Advertisement
here2share

# Tk_parallel_pixel_multiprocessing.py ^2018.08

Jun 12th, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. # Tk_parallel_pixel_multiprocessing.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. xy = [y for y in range(he)]
  20.  
  21. def thread_process(y):
  22.     return [(0,random.randrange(y*2+15, 255),0) for x in range(wi)]
  23.  
  24. if __name__ == '__main__':
  25.     pool = Pool(processes=(cpu_count() - 1))
  26.     img = Image.new( 'RGB', (wi,he))
  27.     while 1:
  28.         p = pool.map(thread_process, xy)
  29.         data = []
  30.         for d in p:
  31.             data.extend(d)
  32.         img.putdata(data)
  33.         imgTk = ImageTk.PhotoImage(img)
  34.        
  35.         ### saw no glitches
  36.         #time.sleep(0.02)
  37.        
  38.         w.create_image(0, 0, anchor=NW, image=imgTk)
  39.         root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement