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

David M. Cotter me at davecotter.com
Sun Jul 28 21:21:25 CEST 2013

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;
		if (err) {
			resultI = -1;
		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;

		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?

More information about the Python-list mailing list