Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51400 > unrolled thread

embedding: how to create an "idle" handler to allow user to kill scripts?

Started by"David M. Cotter" <me@davecotter.com>
First post2013-07-28 12:21 -0700
Last post2013-07-30 12:56 -0700
Articles 2 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  embedding: how to create an "idle" handler to allow user to kill scripts? "David M. Cotter" <me@davecotter.com> - 2013-07-28 12:21 -0700
    Re: embedding: how to create an "idle" handler to allow user to kill scripts? "David M. Cotter" <me@davecotter.com> - 2013-07-30 12:56 -0700

#51400 — embedding: how to create an "idle" handler to allow user to kill scripts?

From"David M. Cotter" <me@davecotter.com>
Date2013-07-28 12:21 -0700
Subjectembedding: how to create an "idle" handler to allow user to kill scripts?
Message-ID<55e03ef8-d1db-4ae0-88a5-7641fb4b1b57@googlegroups.com>
in my C++ app, on the main thread i init python, init threads, then call PyEval_SaveThread(), since i'm not going to do any more python on the main thread.

then when the user invokes a script, i launch a preemptive thread (boost::threads), and from there, i have this:

	static int		CB_S_Idle(void *in_thiz) {
		CT_RunScript		*thiz((CT_RunScript *)in_thiz);
		
		return thiz->Idle();
	}
	
	int			Idle()
	{
		int		resultI = 0;
		OSStatus	err = noErr;
		
		ERR(i_taskRecP->MT_UpdateData(&i_progData));
		
		if (err) {
			resultI = -1;
		}
		
		ERR(ScheduleIdleCall());
		return err;
	}
	
	int			ScheduleIdleCall()
	{
		int		resultI(Py_AddPendingCall(CB_S_Idle, this));
		CFAbsoluteTime	timeT(CFAbsoluteTimeGetCurrent());
		SuperString	str; str.Set(timeT, SS_Time_LOG);
		
		Logf("$$$ Python idle: (%d) %s\n", resultI, str.utf8Z());
		return resultI;
	}
	
	virtual	OSStatus	operator()(OSStatus err) {
		ScPyGILState		sc;

		ERR(ScheduleIdleCall());
		ERR(PyRun_SimpleString(i_script.utf8Z()));
		return err;
	}

so, my operator() gets called, and i try to schedule an Idle call, which succeeds, then i run my script.  however, the CB_S_Idle() never gets called?

the MT_UpdateData() function returns an error if the user had canceled the script

must i schedule a run-loop on the main thread or something to get it to be called?

[toc] | [next] | [standalone]


#51601

From"David M. Cotter" <me@davecotter.com>
Date2013-07-30 12:56 -0700
Message-ID<fe3f4cfd-28f7-450f-a98e-30c071e3967a@googlegroups.com>
In reply to#51400
Okay, i'm really surprised nobody knows how to do this.  and frankly i'm amazed at the utter lack of documentation.  but i've figured it out, and it's all working beautifully.

if you want the code, go here:
http://karaoke.kjams.com/wiki/Python

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web