def test_random_addr(): ctx = zproc.Context() ctx.state["foo"] = 42 ctx = zproc.Context(ctx.server_address) assert ctx.state.copy() == {"foo": 42} state = zproc.State(ctx.server_address) assert state.copy() == {"foo": 42}
def test_random_addr(): ctx = zproc.Context() state = ctx.create_state(TEST_VALUE) ctx = zproc.Context(ctx.server_address, start_server=False) assert state == TEST_VALUE state = zproc.State(ctx.server_address) assert state == TEST_VALUE
def test_start_server(): _, addr = zproc.start_server() ctx = zproc.Context(addr, start_server=False) state = ctx.create_state(TEST_VALUE) ctx = zproc.Context(addr, start_server=False) assert state == TEST_VALUE state = zproc.State(ctx.server_address) assert state == TEST_VALUE
def child2(state): print("level2:", state) state["msg"] = "hello from level2" ctx = zproc.Context(state.server_address, wait=True) @ctx.process def child3(state): print("level3:", state)
def state() -> zproc.State: ctx = zproc.Context() @ctx.process def mutator(state: zproc.State): for i in range(10): state["counter"] = i sleep(0.25) return ctx.state
def test_static_addr(): addr = "tcp://127.0.0.1:%d" % random.randint(20000, 50000) ctx = zproc.Context(addr) state = ctx.create_state(TEST_VALUE) assert state == TEST_VALUE state = zproc.State(addr) assert state == TEST_VALUE
def test_static_addr(): zproc.start_server(ADDRESS) ctx = zproc.Context(ADDRESS) ctx.state["foo"] = 42 assert ctx.state.copy() == {"foo": 42} state = zproc.State(ADDRESS) assert state.copy() == {"foo": 42}
def state() -> zproc.State: ctx = zproc.Context() @ctx.spawn def mutator(ctx: zproc.Context): state = ctx.create_state() for n in range(10): sleep(0.1) state["counter"] = n return ctx.create_state()
def _setup_ctx(): ctx = zproc.Context() @ctx.process def updater(state): state["avail"] = True state["none"] = None time.sleep(1) state["true"] = True state["none"] = True return ctx
def test_namespaces(): state = zproc.Context().create_state() state.namespace = "test1" state["foo"] = 10 assert state == {"foo": 10} state.namespace = "test2" state["bar"] = 10 assert state == {"bar": 10} state.namespace = "test3" assert state == {}
def state(): ctx = zproc.Context() @ctx.spawn() def updater(ctx): state = ctx.create_state() state["none"] = None state["flag"] = False time.sleep(0.1) state["avail"] = True state["flag"] = True state["none"] = True return ctx.create_state()
def ctx(): return zproc.Context(stateful=False)
def ctx(): return zproc.Context()
def num_listener(state, low, high): # blocks until num is between the specified range state.when(lambda state: low < state.get("num") < high) print("listener: foo is between {0} and {1}, so I awake".format(low, high)) state["STOP"] = True print("listener: I set STOP to True") print("listener: I exit") if __name__ == "__main__": ctx = zproc.Context() # create a context for us to work with state = ctx.state state.setdefault("num", 0) # set the default value, just to be safe # give the context some processes to work with # also give some props to the num listener ctx.spawn(num_listener, args=[0.6, 0.601]) while True: if state.get("STOP"): print("num gen: STOP was set to True, so lets exit") break else: num = random.random() state["num"] = num
<Process pid: 2555 target: <function cookie_eater at 0x7f5b4542c9d8> uuid: e74521ae-76ca-11e8-bd1f-7c7a912e12b5> <Process pid: 2556 target: <function cookie_baker at 0x7f5b4542c950> uuid: e74521ae-76ca-11e8-bd1f-7c7a912e12b5> Here's a cookie! Here's a cookie! Here's a cookie! nom nom nom Here's a cookie! nom nom nom Here's a cookie! nom nom nom nom nom nom nom nom nom """ import zproc ctx = zproc.Context(wait=True) # background waits for all processes to finish ctx.state["cookies"] = 0 @zproc.atomic def eat_cookie(state): state["cookies"] -= 1 print("nom nom nom") @zproc.atomic def bake_cookie(state): state["cookies"] += 1 print("Here's a cookie!")
import zproc # define a child process def child1(state): val = state.when_change("foo") # wait for foo to change print("child1: foo changed, so I wake, now foo =", val) state["foo"] = "bar" # update bar print("child1: I set foo to bar") print("child1: I exit") # define another child process def child2(state): state.when(lambda s: s.get("foo") == "bar") # wait for bar_equals_bar print("child2: foo changed to bar, so I wake") print("child2: I exit") if __name__ == "__main__": ctx = zproc.Context(wait=True) # create a context for us to work with ctx.spawn(child1, child2) # give the context some processes to work with sleep(1) # sleep for no reason ctx.state["foo"] = "foobar" # set initial state print("main: I set foo to foobar") print("main: I exit")
Expected Output: 1 2 3 . . 100 """ from random import random from time import sleep import zproc ctx = zproc.Context(wait=True) ctx.state["count"] = 0 @zproc.atomic def increment(snap): count = snap["count"] sleep(random()) snap["count"] = count + 1 print(snap["count"]) def child1(state): increment(state)
def test_not_start_server(): with pytest.raises(AssertionError): zproc.Context(start_server=False)
import time import zproc ctx = zproc.Context() for i in range(250): @ctx.spawn def p1(ctx): @ctx.spawn def p2(ctx): @ctx.spawn def p3(ctx): @ctx.spawn def p4(ctx): @ctx.spawn(pass_context=False) def pn(): time.sleep(1) print(i) return i assert p1.wait() == i
def ctx(): return zproc.Context(pass_context=False)
import zproc swarm = zproc.Context().create_swarm() fn = lambda x: x i = 0 while True: assert swarm.map(fn, range(10**5)) == list(map(fn, range(10**5))) print(i) i += 1
def main(): ctx = zproc.Context(wait=True, retry_for=(Exception,)) ctx.state["volume"] = 100 ctx.state["brightness"] = 100 @ctx.process def network(state): with unetwork.Peer(settings.udp_port) as peer: while True: dict_data = unpack(peer.recv()[0]) log.debug(dict_data) state.update(dict_data) @ctx.process def update_volume(state): with pulsectl.Pulse("muro-volume") as pulse: while True: volume = state.get_when_change("volume")["volume"] set_volume(pulse, volume) @ctx.call_when_change("pause", stateful=False) def play_pause(_): send_command_to_player("play-pause") @ctx.call_when_change("brightness", stateful=False) def update_brightness(snapshot): set_brightness(snapshot["brightness"]) def seek_btn_process_gen(key, cmd, seek_range): if cmd == "next": seek_cmds = [ f"{i:.2f}+" for i in np.geomspace(seek_range[0], seek_range[1], 50) ] elif cmd == "previous": seek_cmds = [ f"{i:.2f}-" for i in np.geomspace(seek_range[0], seek_range[1], 50) ] else: raise ValueError( f'"cmd" must be one of "next" or "previous", not {repr(cmd)}' ) @ctx.call_when_equal(key, True) def seek_btn_process(_, state): log.debug(f"{key} btn pressed") try: state.get_when_equal(key, False, timeout=settings.Buttons.seek_timeout) send_command_to_player(cmd) except TimeoutError as e: log.debug(e) log.info("seek forward...") seek_cmd_it = LastValueIterator(seek_cmds) while state[key]: send_command_to_player("position", next(seek_cmd_it)) sleep(0.1) log.debug(f"{key} btn released") seek_btn_process_gen("next", "next", (2, 10)) seek_btn_process_gen("previous", "previous", (3, 10)) pprint(ctx.process_list)