Writing to function arguments during execution

John O'Hagan research at johnohagan.com
Sun Oct 11 09:18:25 EDT 2009


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




More information about the Python-list mailing list