multiprocessing

This commit is contained in:
2020-02-05 16:01:25 +01:00
parent 5f08380e94
commit 0cf4d6635a

View File

@@ -3,19 +3,16 @@ import socket
import PIL import PIL
from PIL import Image, ImageColor from PIL import Image, ImageColor
import argparse import argparse
import threading import multiprocessing
import random
from pprint import pprint
from sys import exit from sys import exit
sizex = 800 sizex = 800
sizey = 600 sizey = 600
XSPLIT = 5
YSPLIT = 5
threads = [] threads = []
dirtybox = (0,0,sizex,sizey)
old_framebuffer = Image.new("RGBA", (sizex, sizey), None) old_framebuffer = Image.new("RGBA", (sizex, sizey), None)
framebuffer = Image.new("RGBA", (sizex, sizey), (0,0,0,0)) framebuffer = Image.new("RGBA", (sizex, sizey), (0,0,0,0))
@@ -31,15 +28,29 @@ effect = parser.add_mutually_exclusive_group(required=False)
effect.add_argument("--repeat", action="store_true") effect.add_argument("--repeat", action="store_true")
effect.add_argument("--animate", type=int, const=5, nargs='?', metavar="STEPSIZE") effect.add_argument("--animate", type=int, const=5, nargs='?', metavar="STEPSIZE")
parser.add_argument("--bounce", action="store_true") parser.add_argument("--bounce", action="store_true")
parser.add_argument("--debug", action="store_true")
size = parser.add_mutually_exclusive_group(required=False)
size.add_argument("--size", default=[800, 600], type=int, nargs=2)
size.add_argument("--autodetect", action="store_true")
parser.add_argument("--threads", type=int, default="5")
args = parser.parse_args() args = parser.parse_args()
pprint(args)
sizex = args.size[0]
sizey = args.size[1]
dirtybox = (0,0,sizex,sizey)
XSPLIT = args.threads
YSPLIT = args.threads
if args.bounce and not args.animate: if args.bounce and not args.animate:
print("--bounce requires --animate") print("--bounce requires --animate")
exit(1) exit(1)
class DrawThread(multiprocessing.Process):
class DrawThread(threading.Thread):
def run(self): def run(self):
xfrom = self._kwargs["xfrom"] xfrom = self._kwargs["xfrom"]
yfrom = self._kwargs["yfrom"] yfrom = self._kwargs["yfrom"]
@@ -51,6 +62,13 @@ class DrawThread(threading.Thread):
buf = bytearray() buf = bytearray()
global framebuffer global framebuffer
if args.debug:
for x in range(xfrom,xto):
for y in range(yfrom,yto):
if framebuffer.getpixel((x,y))[3] != 0:
value = b"%02X%02X%02X%02X"%self._kwargs["color"]
buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value)
else:
for x in range(xfrom,xto): for x in range(xfrom,xto):
for y in range(yfrom,yto): for y in range(yfrom,yto):
if framebuffer.getpixel((x,y))[3] != 0: if framebuffer.getpixel((x,y))[3] != 0:
@@ -109,6 +127,10 @@ def draw():
for xchunk in xchunks: for xchunk in xchunks:
for ychunk in ychunks: for ychunk in ychunks:
if args.debug:
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255), 255)
threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1], "color": color}))
else:
threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]})) threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]}))
for thread in threads: for thread in threads: