![]() |
How can I print from within actor(func) ?? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I print from within actor(func) ?? (/thread-42054.html) |
How can I print from within actor(func) ?? - Pedroski55 - Apr-30-2024 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. #! /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 RE: How can I print from within actor(func) ?? - deanhystad - Apr-30-2024 def actor(func): def wrapper(*args, **kwargs): gen = func(*args, **kwargs) next(gen) _registry[func.__name__] = gen print(f'key in _registry is {func.__name__}') return wrapperor def actor(func): def wrapper(*args, **kwargs): gen = func(*args, **kwargs) next(gen) _registry[func.__name__] = gen print(f'key in _registry is {func.__name__}') return wrapperEither spot I get this for output. What is the problem that I am not seeing?
RE: How can I print from within actor(func) ?? - Pedroski55 - May-01-2024 Thanks for the reply. The problem is probably Idle. When I do this in Idle, def actor(func): # this will not print #print(f'key in _registry is {func.__name__}') def wrapper(*args, **kwargs): gen = func(*args, **kwargs) next(gen) _registry[func.__name__] = gen print(f'key in _registry is {func.__name__}') return wrapper printerQ() None n = 10 while n > 0: send('printerQ', n) n -= 1I get: But in bash it prints: Should have tried it in bash as well!
|