croterline/tests/test_subprocess.py

45 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-10-12 23:35:01 +08:00
import time
from croterline.context import Context
from croterline.process import SubProcess, get_ctx
def p_func(*args, **kwargs):
2024-10-13 17:28:37 +08:00
print("Args", args)
print("Kwargs", kwargs)
2024-10-12 23:35:01 +08:00
i = 0
ctx = get_ctx()
while True:
ctx.sub_chan << "Running" << "SubProcess"
print("Recv from main: ", str << ctx.main_chan)
i += 1
if i == 5:
ctx.sub_chan << "end"
2024-10-13 17:28:37 +08:00
def p_func2(*args, **kwargs):
print("args: ", args)
print("kwargs: ", kwargs)
raise Exception("Test")
2024-10-12 23:35:01 +08:00
class TestSubProcess:
def test_run(self):
print("start")
2024-10-13 17:28:37 +08:00
sp = SubProcess("test", p_func, Context(), 1, 2, 3, k1=1, k2=2)
2024-10-12 23:35:01 +08:00
sp.start()
while True:
s = str << sp.ctx.sub_chan
sp.ctx.main_chan << "Resp"
print("Recv from sub: ", s)
if s == "end":
break
print("finished")
sp.terminate()
2024-10-13 17:28:37 +08:00
def test_input(self):
print("test_input")
sp = SubProcess("test2", p_func2, Context(), 1, 2, 3, host=1, port=2)
sp.start()