Demonstrate multiprocess execution

This commit is contained in:
lash 2024-10-27 23:07:20 +00:00
parent 7494f09d61
commit f803256ee9
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -2,6 +2,14 @@ import ply.lex as lex
import ply.yacc as yacc
import enum
import uuid
import os
import logging
from multiprocessing import Process
from multiprocessing import Value
from multiprocessing import Pipe
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
tokens = (
'ID',
@ -236,14 +244,73 @@ def p_pairnoid(p):
p[0] = o
class Router:
def __init__(self):
self.__routes = {}
self.__r = {}
def register(self, cmd_id, fn):
self.__routes[cmd_id] = fn
def sync(self, uid):
o = self.__r[uid]
if o[1] == None:
return None
r = o[1][0].recv()
o = (o[0], None,)
o[0].join()
return r
def __wrap(self, uid, fn, cmd):
r = fn(cmd)
self.__r[uid][1][1].send(r)
def exec(self, cmd):
if cmd.c & 0xa0 > 0:
return self.sync(cmd.i)
fn = self.__routes[cmd.c]
pi = Pipe(False)
po = Process(target=self.__wrap, args=(cmd.i, fn, cmd,))
self.__r[cmd.i] = (po, pi,)
po.start()
def __del__(self):
for k, v in self.__r.items():
r = self.sync(k)
logging.debug("synced " + k + ": " + r)
def noop_handler(cmd):
return str(cmd)
def foo_handler(cmd):
return os.popen('eth-info -p https://celo.grassecon.net').read()
parser = yacc.yacc()
print("result: {}".format(parser.parse("WAIT bar-baz")))
print("result: {}".format(parser.parse("KEY_CREATE bar-baz Foo BAr")))
print("result: {}".format(parser.parse("KEY_CREATE bar-baz Foo BAr 0xabcdef0")))
print("result: {}".format(parser.parse("VOUCHER_MINT - 0xdeadbeef 13 0xfdaf")))
print("result: {}".format(parser.parse("VOUCHER_MINT bar-baz 0xdeadbeef 13 0xfdaf")))
print("result: {}".format(parser.parse("VOUCHER_MINT bar-baz 0xdeadbeef 42")))
print("result: {}".format(parser.parse("VOUCHER_CREATE bar-baz Foo Bar 42")))
print("result: {}".format(parser.parse("VOUCHER_CREATE bar-baz Foo Bar 42 44 233")))
print("result: {}".format(parser.parse("VOUCHER_TRANSFER bar-baz 0xbeeffeed0123 666 foo 0x0fdc")))
cmds = [
"KEY_CREATE foo-bar Foo BAr",
"KEY_CREATE bar-baz Foo BAr 0xabcdef0",
"VOUCHER_MINT - 0xdeadbeef 13 0xfdaf",
"VOUCHER_MINT baz-baz 0xdeadbeef 13 0xfdaf",
"VOUCHER_MINT bar-bar 0xdeadbeef 42",
"VOUCHER_CREATE foo-foo Foo Bar 42",
"VOUCHER_CREATE inky-pinky-blinky Foo Bar 42 44 233",
"VOUCHER_TRANSFER clyde-sue 0xbeeffeed0123 666 foo 0x0fdc",
]
for v in cmds:
o = Router()
o.register(CmdId.KEY_CREATE, noop_handler)
o.register(CmdId.VOUCHER_MINT, noop_handler)
o.register(CmdId.VOUCHER_CREATE, noop_handler)
o.register(CmdId.VOUCHER_TRANSFER, foo_handler)
r = parser.parse(v)
o.exec(r)