multithreading for sending

main
Felix Pankratz 3 years ago
parent 774f4fd877
commit 92bacb568c

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -5,9 +5,10 @@ import math
import random
import threading
import time
import socket
from utils import circle_fill
from utils import random_color
from pixelflut import surface_to_pixelflut
#from pixelflut import surface_to_pixelflut
WIDTH, HEIGHT = 1920, 1080
ANGLE_RANDOM_MIN = -0.6
@ -17,6 +18,42 @@ SHRINK = 0.00002
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():
def __init__(self, idx, ctx, x, y, r, ang):
@ -45,7 +82,7 @@ class Branch():
return False
# did we hit canvas edge?
# 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
return False
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):
source = ctx.get_source()
new_subs = []
threads = list()
#threads = list()
print('spawning sub growing threads')
for branch in 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()
print('all threads finished')
print('starting place_next threads')
pl_threads = list()
#pl_threads = list()
for branch in new_subs:
x = threading.Thread(target=grow_branch_until_ended, args=(branch, branches))
pl_threads.append(x)
x.start()
for tidx, thread in enumerate(pl_threads):
thread.join()
grow_branch_until_ended(branch, branches)
print('place_next done')
#while not all([branch.ended for branch in new_subs]):
@ -140,6 +173,7 @@ def grow_subs(ctx, subs, branches):
return new_subs
def main():
global DRAW_MAP
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
@ -165,6 +199,9 @@ def main():
# start branching off
#subs = []
subs = branches
DRAW_MAP = surface
t = threading.Thread(target=surface_to_pixelflut, daemon=True)
t.start()
try:
for x in range(8):
if subs == None:
@ -172,16 +209,13 @@ def main():
r, g, b = random_color()
ctx.set_source_rgb(r, g, b)
subs = grow_subs(ctx, subs, branches)
print(f'iteration {x} done, sending to pixelflut')
x = threading.Thread(target=surface_to_pixelflut, args=(surface,))
x.daemon = True
x.start()
x.join()
#surface_to_pixelflut(surface)
DRAW_MAP = surface
#surface.write_to_png("out/hyphae.png") # Output to PNG
#input('next')
finally:
surface.write_to_png("out/hyphae.png") # Output to PNG
sys.exit(0)
if __name__ == '__main__':
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 661 KiB

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

Loading…
Cancel
Save