Writing to function arguments during execution

Discussion in 'Python' started by John O'Hagan, Oct 11, 2009.

  1. John O'Hagan

    John O'Hagan Guest

    I'm writing a (music-generating) program incorporating a generator function
    which takes dictionaries as its arguments. I want to be able to change the
    values of the arguments while the program is running. I have it working as in
    this toy example (python 2.5):

    from sys import argv
    from threading import Thread
    from my_functions import option_processor, work

    #Make a dictionary of arguments to the main "work" function
    argdict = option_processor(argv[1:])

    def argdict_rewriter(argdict):
    """Write new values to a dictionary of arguments"""
    while 1:
    new_dict = option_processor(raw_input().split())
    argdict.update(new_dict)

    #Write to the dictionary while program is running
    rewriter = Thread(target=argdict_rewriter, args=(argdict,))
    rewriter.setDaemon(True)
    rewriter.start()

    #The main generator function
    work(argdict)

    Now I can change the output of the "work" function while it's running via
    raw_input(). However it's very crude, not least because the terminal echo of
    the new options is interspersed with the output of the program.

    In future I hope to be able to have several instances of the "work" function
    running as threads simultaneously, and to separately control the arguments to
    each.

    I think the general problem is how to send output from a thread to a different
    place from that of its parent thread, but I'm not sure.

    Is there a standard way to do this kind of thing? In particular, I'm after a
    solution whereby I can enter new arguments in one terminal window and observe
    the program's output in another.

    Regards,

    John
    John O'Hagan, Oct 11, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    10
    Views:
    655
    sushant
    Jan 20, 2005
  2. tutmann
    Replies:
    4
    Views:
    434
  3. jmborr
    Replies:
    1
    Views:
    407
    Stargaming
    Nov 3, 2007
  4. Replies:
    3
    Views:
    812
  5. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
Loading...

Share This Page