multithreading for sending

This commit is contained in:
Felix Pankratz 2021-12-29 16:02:18 +01:00
parent 774f4fd877
commit 92bacb568c
14 changed files with 67 additions and 41 deletions

BIN
.hyphae_pixelflut.py.swp Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,9 +5,10 @@ import math
import random import random
import threading import threading
import time import time
import socket
from utils import circle_fill from utils import circle_fill
from utils import random_color from utils import random_color
from pixelflut import surface_to_pixelflut #from pixelflut import surface_to_pixelflut
WIDTH, HEIGHT = 1920, 1080 WIDTH, HEIGHT = 1920, 1080
ANGLE_RANDOM_MIN = -0.6 ANGLE_RANDOM_MIN = -0.6
@ -17,6 +18,42 @@ SHRINK = 0.00002
hitmap = list() hitmap = list()
DRAW_MAP = None
SERVER_IP = '192.168.178.75'
#SERVER_IP = '127.0.0.1'
SERVER_PORT = 1234
#SERVER_PORT = 1337
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def surface_to_pixelflut():
global DRAW_MAP
while True:
data = DRAW_MAP.get_data()
pixels = data.hex()
x, y = 0, 0
#to_send = list()
pxstr = ''
for hexpx in chunker(pixels, 8):
if x > 1919:
x = 0
y += 1
x += 1
if hexpx[6:8] == '00':
continue
pxstr += f'PX {x} {y} {hexpx[:6]}\n'
#to_send.append(pxstr)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((SERVER_IP, SERVER_PORT))
print('!! socket opened !!')
#for px in to_send:
# s.sendall(px.encode())
s.sendall(pxstr.encode())
#print(f'sent {pxstr}')
print('!! transmission to pixelflut finished !!')
class Branch(): class Branch():
def __init__(self, idx, ctx, x, y, r, ang): def __init__(self, idx, ctx, x, y, r, ang):
@ -45,7 +82,7 @@ class Branch():
return False return False
# did we hit canvas edge? # did we hit canvas edge?
# if next_x + next_r > 1 or next_x - next_r < 0: # if next_x + next_r > 1 or next_x - next_r < 0:
if (math.pow(next_x - 0.5, 2) + math.pow(next_y - 0.5, 2)) > math.pow(0.5, 2): if (math.pow(next_x - 0.5, 2) + math.pow(next_y - 0.5, 2)) > math.pow(0.4, 2):
self.ended = True self.ended = True
return False return False
if next_y + next_r > 1 or next_y - next_r < 0: if next_y + next_r > 1 or next_y - next_r < 0:
@ -114,7 +151,7 @@ def grow_branch_until_ended(branch, branches):
def grow_subs(ctx, subs, branches): def grow_subs(ctx, subs, branches):
source = ctx.get_source() source = ctx.get_source()
new_subs = [] new_subs = []
threads = list() #threads = list()
print('spawning sub growing threads') print('spawning sub growing threads')
for branch in subs: for branch in subs:
# x = threading.Thread(target=grow_sub, args=(ctx, branch, branches, new_subs)) # x = threading.Thread(target=grow_sub, args=(ctx, branch, branches, new_subs))
@ -125,13 +162,9 @@ def grow_subs(ctx, subs, branches):
# thread.join() # thread.join()
print('all threads finished') print('all threads finished')
print('starting place_next threads') print('starting place_next threads')
pl_threads = list() #pl_threads = list()
for branch in new_subs: for branch in new_subs:
x = threading.Thread(target=grow_branch_until_ended, args=(branch, branches)) grow_branch_until_ended(branch, branches)
pl_threads.append(x)
x.start()
for tidx, thread in enumerate(pl_threads):
thread.join()
print('place_next done') print('place_next done')
#while not all([branch.ended for branch in new_subs]): #while not all([branch.ended for branch in new_subs]):
@ -140,6 +173,7 @@ def grow_subs(ctx, subs, branches):
return new_subs return new_subs
def main(): def main():
global DRAW_MAP
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface) ctx = cairo.Context(surface)
@ -165,6 +199,9 @@ def main():
# start branching off # start branching off
#subs = [] #subs = []
subs = branches subs = branches
DRAW_MAP = surface
t = threading.Thread(target=surface_to_pixelflut, daemon=True)
t.start()
try: try:
for x in range(8): for x in range(8):
if subs == None: if subs == None:
@ -172,16 +209,13 @@ def main():
r, g, b = random_color() r, g, b = random_color()
ctx.set_source_rgb(r, g, b) ctx.set_source_rgb(r, g, b)
subs = grow_subs(ctx, subs, branches) subs = grow_subs(ctx, subs, branches)
print(f'iteration {x} done, sending to pixelflut') print(f'iteration {x} done, sending to pixelflut')
x = threading.Thread(target=surface_to_pixelflut, args=(surface,)) DRAW_MAP = surface
x.daemon = True
x.start()
x.join()
#surface_to_pixelflut(surface)
#surface.write_to_png("out/hyphae.png") # Output to PNG #surface.write_to_png("out/hyphae.png") # Output to PNG
#input('next')
finally: finally:
surface.write_to_png("out/hyphae.png") # Output to PNG surface.write_to_png("out/hyphae.png") # Output to PNG
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

BIN
out/hyphae.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 661 KiB

View File

@ -5,41 +5,33 @@ import socket
sys.path.append('..') sys.path.append('..')
from waves import create_wpotd from waves import create_wpotd
SERVER_IP = '192.168.178.75' #SERVER_IP = '192.168.178.75'
SERVER_IP = '127.0.0.1'
SERVER_PORT = 1234 SERVER_PORT = 1234
def chunker(seq, size): def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size)) return (seq[pos:pos + size] for pos in range(0, len(seq), size))
#buf = io.BytesIO() def surface_to_pixelflut():
#surface = create_wpotd(buf) global DRAW_MAP
# while True:
#data = surface.get_data() data = DRAW_MAP.get_data()
#pixels = data.hex()
#
#x, y = 0, 0
#for hexpx in chunker(pixels, 8):
# if x > 1919:
# x = 0
# y += 1
# pxstr = f'PX {x} {y} {hexpx[:6]}'
# x += 1
# print(pxstr)
def surface_to_pixelflut(surface):
data = surface.get_data()
pixels = data.hex() pixels = data.hex()
x, y = 0, 0 x, y = 0, 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: to_send = list()
s.connect((SERVER_IP, SERVER_PORT))
for hexpx in chunker(pixels, 8): for hexpx in chunker(pixels, 8):
if x > 1919: if x > 1919:
x = 0 x = 0
y += 1 y += 1
pxstr = f'PX {x} {y} {hexpx[:6]}\n'
x += 1 x += 1
if hexpx[6:8] == '00': if hexpx[6:8] == '00':
continue continue
s.sendall(pxstr.encode()) pxstr = f'PX {x} {y} {hexpx[:6]}\n'
print('transmission to pixelflut finished') to_send.append(pxstr)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((SERVER_IP, SERVER_PORT))
print('!! socket opened !!')
for px in to_send:
s.sendall(px.encode())
#print(f'sent {pxstr}')
print('!! transmission to pixelflut finished !!')