Skip to content

Commit

Permalink
Changed workers to bound to instance rather then class
Browse files Browse the repository at this point in the history
  • Loading branch information
Attumm committed Aug 12, 2024
1 parent 04a9620 commit e7274c5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 432 deletions.
9 changes: 5 additions & 4 deletions examples/example_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
"timeout": 1,
}

box = Meesee(config)

@Meesee.worker()
@box.worker()
def func_a(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_a', worker_id, item))


@Meesee.worker()
@box.worker()
def func_b(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_b', worker_id, item))


@Meesee.worker()
@box.worker()
def func_c(item, worker_id):
print('func: {}, worker_id: {}, item: {}'.format('func_c', worker_id, item))


if __name__ == '__main__':
workers = int(sys.argv[sys.argv.index('-w') + 1]) if '-w' in sys.argv else 10
Meesee.start_workers(workers=workers, config=config)
box.start_workers(workers=workers, config=config)
23 changes: 10 additions & 13 deletions meesee.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ def __len__(self):


class Meesee:
worker_funcs = {}

def __init__(self, workers=10, namespace="main", timeout=None, queue="main", redis_config={}):
self.workers = workers
self.namespace = namespace
self.timeout = timeout
self.queue = queue
self.redis_config = redis_config
self.worker_funcs = {}

def create_produce_config(self):
return {
Expand Down Expand Up @@ -158,28 +158,25 @@ def wrapper(*args, **kwargs):
return wrapper
return decorator

@staticmethod
def parse_func_name(func):
def parse_func_name(self, func):
return func.__name__

@classmethod
def worker(cls, queue=None):
def worker(self, queue=None):
def decorator(func):
parsed_name = queue if queue is not None else cls.parse_func_name(func)
cls.worker_funcs[parsed_name] = func
parsed_name = queue if queue is not None else self.parse_func_name(func)
self.worker_funcs[parsed_name] = func
return func
return decorator

@classmethod
def start_workers(cls, workers=10, config=config):
n_workers = len(cls.worker_funcs)
def start_workers(self, workers=10, config=config):
n_workers = len(self.worker_funcs)
if n_workers == 0:
print("No workers have been assigned with a decorator")
if n_workers > workers:
print(f"Not enough workers, increasing the workers started with: {workers} we need atleast: {n_workers}")
workers = n_workers

startapp(list(cls.worker_funcs.values()), workers=workers, config=config)
startapp(list(self.worker_funcs.values()), workers=workers, config=config)

def push_button(self, workers=None, wait=None):
if workers is not None:
Expand All @@ -189,13 +186,13 @@ def push_button(self, workers=None, wait=None):
"key": queue,
"namespace": self.namespace,
"redis_config": self.redis_config,
} for queue in self.__class__.worker_funcs.keys()
} for queue in self.worker_funcs.keys()
]
if self.timeout is not None or wait is not None:
for config in configs:
config["timeout"] = self.timeout or wait

startapp(list(self.__class__.worker_funcs.values()), workers=self.workers, config=configs)
startapp(list(self.worker_funcs.values()), workers=self.workers, config=configs)


class InitFail(Exception):
Expand Down
88 changes: 0 additions & 88 deletions meesee_methods_tests.py

This file was deleted.

138 changes: 0 additions & 138 deletions meesee_types_tests.py

This file was deleted.

Loading

0 comments on commit e7274c5

Please sign in to comment.