Apr-30-2024, 07:51 AM
I have the following example from here.
To help me understand what is going on, I like to put print() statements in various places, at least until everything works.
func.__name__ gets the name of the func parameter passed to actor(func) as a string.
I tried putting print(func.__name__) in various places in the function actor(), but it does not print. Generators are tricky!
How can I print(f'key in _registry is {func.__name__}') from within actor(func) ??
This Python routine works fine and does what it should.
To help me understand what is going on, I like to put print() statements in various places, at least until everything works.
func.__name__ gets the name of the func parameter passed to actor(func) as a string.
I tried putting print(func.__name__) in various places in the function actor(), but it does not print. Generators are tricky!
How can I print(f'key in _registry is {func.__name__}') from within actor(func) ??
This Python routine works fine and does what it should.
#! /usr/bin/python3 # actor1.py # # Simple attempt at actors _registry = { } # _registry[name] is a generator # this function was called send() which was confusing, given .send def data(name, msg): # send msg to the generator, which will yield it! What is the point? # maybe with some other function this will be useful _registry[name].send(msg) # can't get actor to print func.__name__ # tried putting this in various places: print(f'key in _registry is {func.__name__}') def actor(func): def wrapper(*args, **kwargs): gen = func(*args, **kwargs) next(gen) _registry[func.__name__] = gen return wrapper if __name__ == '__main__': @actor def printer(): while True: msg = yield print('printer:', msg) # initiate the genrator printer() # initiate the _registry printer() # produces {'printerQ': <generator object printerQ at 0x7b11d97329d0>} n = 10 while n > 0: data('printer', n) n -= 1