Python Forum
How can I print from within actor(func) ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I print from within actor(func) ??
#1
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
Reply


Messages In This Thread
How can I print from within actor(func) ?? - by Pedroski55 - Apr-30-2024, 07:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,513 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 1,643 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,473 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  How do I create a actor if I subscribe to a specific topic? sdf1444 0 1,783 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  call func from dict mcmxl22 3 3,041 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 3,828 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,477 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 6,236 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  How can I return my list from a func? Mike Ru 3 3,245 Oct-22-2018, 01:15 PM
Last Post: buran
  list func in lambda mepyyeti 1 3,026 Mar-10-2018, 09:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020