pyrun — Run a Python statement or block of statements.
pyrun "statement"
pyruni "statement"
pylrun "statement"
pylruni "statement"
pyrunt ktrigger, "statement"
pylrunt ktrigger, "statement"
Execute the specified Python statement at k-time (pyrun and pylrun) or i-time (pyruni and pylruni).
The statement is executed in the global environment for pyrun and pyruni or the local environment for pylrun and pylruni.
These opcodes perform no message passing. However, since the statement have access to the main namespace and the private namespace, it can interact with objects previously created in that environment.
The "local" version of the pyrun opcodes are useful when the code ran by different instances of an instrument should not interact.
Example 728. Orchestra
sr=44100 kr=4410 ksmps=10 nchnls=1 ;If you're not running CsoundAC you need the following line ;to initialize the python interpreter ;pyinit pyruni "import random" instr 1 ; This message is stored in the main namespace ; and is the same for every instance pyruni "message = 'a global random number: %f' % random.random()" pyrun "print message" ; This message is stored in the private namespace ; and is different for different instances pylruni "message = 'a private random number: %f' % random.random()" pylrun "print message" endin
Running this score you should get intermixed pairs of messages from the two instances of instrument 1.
The first message of each pair is stored into the main namespace and so the second instance overwrites the message of the first instance. The result is that first message will be the same for both instances.
The second message is different for the two instances, being stored in the private namespace.