[Cs-dev] python opcodes and exit
Date | 2011-01-27 11:55 |
From | Oeyvind Brandtsegg |
Subject | [Cs-dev] python opcodes and exit |
Hello When using the python opcodes, Csound will not exit on Ctrl+C but continue processing until score is finished. Would it be possible to let it exit on Ctrl+C in this case? Example pasted below, where the csd will play a sine tone for 10 seconds, not responding to Ctrl+C if I remove the "pyinit" statement, Csound will respond as expected (exiting) on Ctrll+C best Oeyvind |
Date | 2011-01-27 13:10 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
Try setting up a signal handler in your Python code, and do a hard exit from it. Regards, Mike On Thu, Jan 27, 2011 at 6:55 AM, Oeyvind Brandtsegg |
Date | 2011-01-27 13:35 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
mm, I'm not totally sure what you mean, but I tried using a function like this in my python code import os def stopMe(bogus): print 'python exit' os._exit(0) and then calling the function from Csound. This stops the python module from working, but does not allow me to exit from Csound. I then realized afterwards that this only stops the module, not python __main__, so I tried putting this in a Csound instrument instr 102 pyruni "print 'python stop'" pyruni "import os" pyruni "os._exit(0)" endin Triggering this instrument, I expected Python to quit (it does) , allowing Csound to exit (does not). I guess you meant something else (?) best, Oeyvind 2011/1/27 Michael Gogins |
Date | 2011-02-09 23:49 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
Ok, this works with Python 2.7, Csound 5.13, Windows 7 either on the command line, or in Idle. import os import os.path import signal import sys import traceback # Import the Csound 5 API. import csnd def signal_handler(signum, frame): print print 'Received signal', signum traceback.print_stack() os._exit(0) for signum in (signal.SIGABRT, signal.SIGFPE, signal.SIGILL, signal.SIGINT, signal.SIGSEGV, signal.SIGTERM): signal.signal(signum, signal_handler) Regards, Mike On Thu, Jan 27, 2011 at 6:55 AM, Oeyvind Brandtsegg |
Date | 2011-02-10 08:00 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
Thanks Michael, hope you will be patient with me on this, it's still a little bit confusing. In what context should I use it ? What confuses me is e.g. the import csnd, as I thought this would be used when running Python from Csound (pyinit), not the other way around. Also, what triggers the signal handler? Is it triggered when I press Ctrl-C to stop Csound ? And is it correct that the os and os.path imports are not used for anything? best Oeyvind 2011/2/10 Michael Gogins |
Date | 2011-02-10 11:08 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
Attachments | None None |
I was confused, I thought you were running python. You can I think set up your signal handler in your pyinit call. Os not used as you said. On Feb 10, 2011 3:00 AM, "Oeyvind Brandtsegg" <oyvind.brandtsegg@ntnu.no> wrote:
> Thanks Michael, > > hope you will be patient with me on this, > it's still a little bit confusing. In what context should I use it ? > What confuses me is e.g. the import csnd, > as I thought this would be used when running Python from Csound > (pyinit), not the other way around. > Also, what triggers the signal handler? Is it triggered when I press > Ctrl-C to stop Csound ? > > And is it correct that the os and os.path imports are not used for anything? > > best > Oeyvind > > 2011/2/10 Michael Gogins <michael.gogins@gmail.com>: >> Ok, this works with Python 2.7, Csound 5.13, Windows 7 either on the >> command line, or in Idle. >> >> import os >> import os.path >> import signal >> import sys >> import traceback >> >> # Import the Csound 5 API. >> >> import csnd >> >> def signal_handler(signum, frame): >> print 'Received signal', signum >> traceback.print_stack() >> os._exit(0) >> >> for signum in (signal.SIGABRT, signal.SIGFPE, signal.SIGILL, >> signal.SIGINT, signal.SIGSEGV, signal.SIGTERM): >> signal.signal(signum, signal_handler) >> >> Regards, >> Mike >> >> On Thu, Jan 27, 2011 at 6:55 AM, Oeyvind Brandtsegg >> <oyvind.brandtsegg@ntnu.no> wrote: >>> Hello >>> >>> When using the python opcodes, Csound will not exit on Ctrl+C but >>> continue processing until score is finished. >>> Would it be possible to let it exit on Ctrl+C in this case? >>> >>> Example pasted below, >>> where the csd will play a sine tone for 10 seconds, not responding to Ctrl+C >>> if I remove the "pyinit" statement, Csound will respond as expected >>> (exiting) on Ctrll+C >>> >>> best >>> Oeyvind >>> >>> <CsoundSynthesizer> >>> <CsOptions> >>> -odac1 -iadc1 -b2048 -B4096 -+rtaudio=pa >>> </CsOptions> >>> <CsInstruments> >>> sr = 44100 >>> ksmps = 10 >>> nchnls = 2 >>> 0dbfs = 1 >>> >>> giSine ftgen 0, 0, 65537, 10, 1 >>> >>> ;*************************************************** >>> ;initialize Python >>> ;*************************************************** >>> pyinit >>> >>> ;*************************************************** >>> ; sine wave instrument >>> ;*************************************************** >>> instr 31 >>> a1 oscil3 0.5, 440, giSine >>> outch 1, a1, 2, a1 >>> endin >>> ;********************************************************************* >>> >>> </CsInstruments> >>> <CsScore> >>> i 31 0 10 >>> e >>> </CsScore> >>> </CsoundSynthesizer> >>> >>> ------------------------------------------------------------------------------ >>> Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)! >>> Finally, a world-class log management solution at an even better price-free! >>> Download using promo code Free_Logger_4_Dev2Dev. Offer expires >>> February 28th, so secure your free ArcSight Logger TODAY! >>> http://p.sf.net/sfu/arcsight-sfd2d >>> _______________________________________________ >>> Csound-devel mailing list >>> Csound-devel@lists.sourceforge.net >>> https://lists.sourceforge.net/lists/listinfo/csound-devel >>> >> >> >> >> -- >> Michael Gogins >> Irreducible Productions >> http://www.michael-gogins.com >> Michael dot Gogins at gmail dot com >> >> ------------------------------------------------------------------------------ >> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE: >> Pinpoint memory and threading errors before they happen. >> Find and fix more than 250 security defects in the development cycle. >> Locate bottlenecks in serial and parallel code that limit performance. >> http://p.sf.net/sfu/intel-dev2devfeb >> _______________________________________________ >> Csound-devel mailing list >> Csound-devel@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/csound-devel >> > > ------------------------------------------------------------------------------ > The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE: > Pinpoint memory and threading errors before they happen. > Find and fix more than 250 security defects in the development cycle. > Locate bottlenecks in serial and parallel code that limit performance. > http://p.sf.net/sfu/intel-dev2devfeb > _______________________________________________ > Csound-devel mailing list > Csound-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/csound-devel |
Date | 2011-02-10 11:18 |
From | Andres Cabrera |
Subject | Re: [Cs-dev] python opcodes and exit |
Hi, Shouldn't the signals received by Csound (in particular SIGTERM) be passed to python by default without user interaction? Cheers, Andres On Thu, Feb 10, 2011 at 11:08 AM, Michael Gogins |
Date | 2011-02-10 23:30 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
Yes, it seems the error might be that the terminate signal is not passed to Python on Ctrl+C, but that Csound will wait until the score is finished before stopping Python. Below is the simplest example I could think of. (if you comment out pyinit, it is possible to stop with Ctrl+C, when pyinit is there, it will play the entire score before stopping) best Oeyvind |
Date | 2011-02-10 23:43 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
Now, this (example below) actually stops when instr 2 is started. I'm sure I tried this before, and it did not work then... ... but, well, it does work now. Was this what you meant from the beginning perhaps, Michael ? It would be nice it Ctrl+C to Csound could trigger an os_exit() in Python, when running Python in Csound via the py opcodes. best Oeyvind |
Date | 2011-02-11 01:52 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
No, this is not what I meant -- though I'm glad it works. First, I did not realize you were talking about Python opcodes -- I hastily assumed you were talking about scripting Csound from Python, as I do all the time. Second, I did mean what I showed in my last example, setting up a signal handler in Python to handle control-C, instead of assuming Csound will handle it. Probably when Python initializes, it reassigns the signals so that what used to work in Csound will no longer work. My code puts that signal handler back in place, I think. Third, I think my example might well work anyway, even for Python opcodes. That is, you could do a pyevali at the beginning of performance to set up the signal handler in Python, just as in my example: result pyevali {{ import os import signal import traceback def signal_handler(signum, frame): print print 'Received signal', signum traceback.print_stack() os._exit(0) for signum in (signal.SIGABRT, signal.SIGFPE, signal.SIGILL, signal.SIGINT, signal.SIGSEGV, signal.SIGTERM): signal.signal(signum, signal_handler) }} On Thu, Feb 10, 2011 at 6:43 PM, Oeyvind Brandtsegg |
Date | 2011-02-11 07:37 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
Ok. Good, now I understand better the signal handler part. However, it does not allow Ctrl+C to exit Csound. Could you try to run the example below and see if it works for you? I had to change pyevali to pyruni, as I got a python exception with syntax error on "import os" when using pyevali. Must have to do with mulitiline parsing or something... Oeyvind |
Date | 2011-02-11 07:40 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
... sorry, I posted the version with pyevali, which will give an error. This is the one I want you to try, to see if you can Ctrl+C exit before the score finishes. |
Date | 2011-02-11 09:16 |
From | Andres Cabrera |
Subject | Re: [Cs-dev] python opcodes and exit |
Hi, I think the error comes from pyevali requiring an expression instead of statements, and since your code is not an expression, no value is returned which crashes Csound. Cheers, Andres On Fri, Feb 11, 2011 at 7:37 AM, Oeyvind Brandtsegg |
Date | 2011-02-11 12:18 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
OK, I tried your example with pyruni and I could not stop the performance. Now I really see what the problem is. And I tried with soundfile output too, so the problem is not caused by PortAudio. I will investigate further. Regards, Mike On Fri, Feb 11, 2011 at 4:16 AM, Andres Cabrera |
Date | 2011-02-12 03:28 |
From | Michael Gogins |
Subject | Re: [Cs-dev] python opcodes and exit |
The follow code does what you want, but not with control-c. If you press any key in the console while Csound is performing, Csound will exit, even if pyinit is firs called. It is amusing that if you do press control-c, nothing at all happens. This indicates that Python is setting itself up to seriously ignore control-c and somehow, with Csound, this can't be turned off by setting up a signal handler in Python. Probably Csound has decided to handle all the signals instead. Obviously this code can be modified to quit on receiving any specific single key press. Regards, Mike |
Date | 2011-02-14 10:32 |
From | Oeyvind Brandtsegg |
Subject | Re: [Cs-dev] python opcodes and exit |
Thanks for the code, that is very nice. An quite amusing that it will not respond to Ctrl+C, however, if I input Ctrl+C and then any other key, it will give a python exception. (here I did Ctrl+C, then released the keys, then pressed space bar) key 32, keydown 1PERF ERROR in instr 2: pyrun: python exception pyrun "import os" note aborted Traceback (most recent call last): File " |