use threading

master
loooph 2023-03-01 03:26:24 +01:00
parent 690e81cea3
commit dcd1633bd9
1 changed files with 22 additions and 19 deletions

View File

@ -1,43 +1,46 @@
from graphviz import Digraph
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import BlockingOSCUDPServer
import threading
import time
edge_attrs = {
'arrowhead': 'vee',
}
g = Digraph('G', filename='graph.gv', format='pdf', engine='circo', strict=True, edge_attr = edge_attrs)
g = Digraph('G', filename='graph.gv', format='png', engine='circo', strict=True, edge_attr = edge_attrs)
# last vertex received
v = -1
# last edge target received
w = -1
# last edge type received
t = ""
g_lock = threading.Lock()
def print_vertex(address, *args):
global v
global g
v = args[args.index('vertex') + 1]
if v == -1:
g = Digraph('G', filename='graph.gv', format='pdf', engine='circo', strict=True, edge_attr = edge_attrs)
with g_lock:
g = Digraph('G', filename='graph.gv', format='pdf', engine='circo', strict=True, edge_attr = edge_attrs)
return
g.node(str(v))
g.render("Test")
with g_lock:
g.node(str(v))
print(args)
def print_edge(address, *args):
global v
global w
global t
v = args[args.index('v') + 1]
w = args[args.index('w') + 1]
t = args[args.index('edge_type') + 1]
print(args)
# print((v,w,t))
if v == -1 or w == -1:
if v == -1:
return
g.edge(str(v), str(w))
g.render("Test")
with g_lock:
g.edge(str(v), str(w))
print(args)
def update_loop():
while True:
g.render("Test")
time.sleep(0.4)
ul_thread = threading.Thread(target=update_loop)
ul_thread.start()
dispatcher = Dispatcher()
dispatcher.map("/edge", print_edge)