bluepyimpl.py (3522B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\uthread2\bluepyimpl.py 2 import time 3 import blue 4 from stacklesslib import locks, main 5 from stackless import getcurrent 6 import weakref 7 import bluepy 8 from uthread2_plugins import BaseUthreadImpl, stacklessimpl, set_implementation, pop_stackless_tracing_enabled 9 10 class BluepyTasklet(stacklessimpl.StacklessTasklet): 11 12 def __init__(self, func, *args, **kwargs): 13 stackless_tracing_enabled, kwargs = pop_stackless_tracing_enabled(kwargs) 14 15 def inner(): 16 getcurrent().localStorage['uthread2_tasklet'] = weakref.ref(self) 17 try: 18 func_name = func.__name__ 19 except AttributeError: 20 try: 21 func_name = func.key 22 except AttributeError: 23 func_name = 'UnnamedCallable' 24 25 with bluepy.Timer('BluepyTasklet::%s' % func_name): 26 func(*args, **kwargs) 27 28 self.tasklet = bluepy.CreateTaskletExt(inner, stackless_tracing_enabled=stackless_tracing_enabled) 29 self.tasklet.method_name = getattr(func, '__name__', 'unknown_method') 30 self.tasklet.module_name = getattr(func, '__module__', 'unknown_module') 31 try: 32 self.tasklet.file_name = func.__code__.co_filename 33 self.tasklet.line_number = func.__code__.co_firstlineno 34 except AttributeError: 35 self.tasklet.file_name = 'unknown_file' 36 self.tasklet.line_number = 0 37 38 def get(self): 39 while self.tasklet.alive: 40 blue.synchro.Yield() 41 main.mainloop.wakeup_tasklets(None) 42 43 44 class _BluepyAutoTasklet(BluepyTasklet): 45 46 def __init__(self, tasklet): 47 self.tasklet = weakref.proxy(tasklet) 48 if hasattr(tasklet, 'localStorage'): 49 tasklet.localStorage['uthread2_tasklet'] = self 50 51 52 class _BluepyUthread(BaseUthreadImpl): 53 54 def __init__(self): 55 BaseUthreadImpl.__init__(self) 56 57 def sleep(self, seconds): 58 blue.synchro.Sleep(seconds * 1000) 59 60 def sleep_sim(self, seconds): 61 blue.synchro.SleepSim(seconds * 1000) 62 63 def start_tasklet(self, func, *args, **kwargs): 64 return BluepyTasklet(func, *args, **kwargs) 65 66 def yield_(self): 67 blue.synchro.Yield() 68 main.mainloop.wakeup_tasklets(None) 69 70 def get_current(self): 71 current = getcurrent() 72 result = getattr(current, 'localStorage', {}).get('uthread2_tasklet', None) 73 if isinstance(result, _BluepyAutoTasklet): 74 return result 75 if result: 76 tasklet = result() 77 if tasklet is not None: 78 return tasklet 79 return _BluepyAutoTasklet(current) 80 81 def wait(self, tasklets, timeout): 82 deadline = time.time() + timeout 83 while time.time() < deadline: 84 alive = [ tasklet.is_alive() for tasklet in tasklets ] 85 if not all(alive): 86 return 87 self.yield_() 88 89 for tasklet in tasklets: 90 try: 91 tasklet.kill() 92 except Exception as e: 93 pass 94 95 def Event(self): 96 return locks.Event() 97 98 def Semaphore(self): 99 return stacklessimpl.StacklessSemaphore() 100 101 def BlockingChannel(self): 102 return stacklessimpl.Channel(-1) 103 104 def PumpChannel(self): 105 return stacklessimpl.Channel(1) 106 107 def QueueChannel(self): 108 return stacklessimpl.QueueChannel() 109 110 111 BluepyImpl = _BluepyUthread() 112 set_implementation(BluepyImpl)