Spaces:
Paused
Paused
| import sys,os | |
| from collections import defaultdict | |
| import multiprocessing | |
| from multiprocessing import Lock, Process, Event | |
| from multiprocessing.managers import SyncManager | |
| from contextlib import contextmanager | |
| class multi_locker: | |
| def __init__(zl): | |
| zl.lock = Lock() | |
| zl.lockli = defaultdict(Lock) | |
| zl.event_lock = Lock() | |
| zl.eventli = defaultdict(Event) | |
| def acquire(zl, name): | |
| with zl.lock: | |
| lock = zl.lockli[name] | |
| lock.acquire() | |
| def release(zl, name): | |
| with zl.lock: | |
| lock = zl.lockli[name] | |
| lock.release() | |
| "event" | |
| def notready(zl, name): | |
| with zl.event_lock: | |
| ev = zl.eventli[name] | |
| ev.clear() | |
| def wait(zl, name, timeout=None): | |
| with zl.event_lock: | |
| ev = zl.eventli[name] | |
| ev.wait(timeout=timeout) | |
| def ready(zl, name): | |
| with zl.event_lock: | |
| ev = zl.eventli[name] | |
| ev.set() | |
| f="" | |
| def fil(zl,*w,**W): | |
| zl.f+=str(w) | |
| return zl.f | |
| def fil2(zl):return zl | |
| # | |
| def fwfw(*w):fwfw | |
| def fin2(*w): | |
| if "need to start with start()": | |
| mana.shutdown() | |
| def fin(*w):sys.exit()# not | |
| "not unicorn" | |
| def meg(zl,name):1 | |
| def locker(zl, name): | |
| return zl.lockli[name] | |
| class maimai(SyncManager): pass | |
| maimai.register('multi_locker', multi_locker) | |
| from multiprocessing.managers import BaseProxy | |
| class pr(BaseProxy): | |
| def acquire(zl, name: str): | |
| return zl._callmethod('acquire', (name,)) | |
| def release(zl, name: str): | |
| return zl._callmethod('release', (name,)) | |
| def __getattr__(zl,at): | |
| def fp(*w,**W): | |
| return zl._callmethod(at, w,W) | |
| return fp | |
| def locker(zl, name: str): | |
| zl.acquire(name) | |
| try: | |
| yield None | |
| finally: | |
| zl.release(name) | |
| if 1: | |
| port=6969 | |
| def main(): | |
| global mana | |
| #maimai.register('n', lambda:multi_locker(), proxytype=pr) | |
| one=multi_locker() | |
| maimai.register('one', lambda:one) | |
| _mana=mana=maimai(("localhost",port),b"p") | |
| mana.get_server().serve_forever() | |
| #mana.start()# cant pickle "lambda" | |
| #mana.shutdown() | |
| print("mana") | |
| return mana | |
| def browser(port): | |
| _mana=mana=maimai(("localhost",port),b"p") | |
| mana.connect() | |
| mana.register('one',None,pr) | |
| return mana.one().locker | |
| def iniport(port): | |
| import subprocess | |
| from mlwebOrWeb import chkbind | |
| if chkbind(port): | |
| subprocess.Popen(["py",__file__, f'{port}']) | |
| from multiprocessing import freeze_support | |
| if __name__ == '__main__': | |
| freeze_support() # ? | |
| main() | |