add commandbuffer
This commit is contained in:
34
grum/cmdbuf.py
Normal file
34
grum/cmdbuf.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from queue import Queue
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
|
class CommandBuffer:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.cmds = Queue()
|
||||||
|
self.thread = None
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
def _run(self):
|
||||||
|
self.running = True
|
||||||
|
while self.running:
|
||||||
|
c = self.cmds.get()
|
||||||
|
func, args, kwargs = c
|
||||||
|
print("run:", func, args, kwargs.keys())
|
||||||
|
func(*args, **kwargs)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.thread = thread = Thread(target=self._run)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
def add(self, func, *args, **kwargs):
|
||||||
|
print("add:", func, args, kwargs.keys())
|
||||||
|
c = (func, args, kwargs)
|
||||||
|
self.cmds.put(c)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user