473,513 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

embedding: forcing an interpreter to end

I'm trying to prototype an application which runs multiple python
scripts, each in its own interpreter and OS thread. I've not been able
to forceable stop a script which does not respond to a request to stop.

My thought was to simply call:

PyThreadState_Clear(xxx);
PyThreadState_Delete(xxx); // Fatal Python error: no last thread
Py_EndInterpreter(xxx);

But, this doesn't work. The call to delete causes a fatal error. If I
skip the call to delete, the call to EndInterpreter() ends but seg
faults abound afterwards. A small sample follows.

I've googled my heart out and reread the threading API countless times.
Any suggest or hints would be greatly appreciated.

----
#include "Python.h"

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

PyThreadState *gtstate;

void * mythread( void *none )
{
PyEval_AcquireLock();
gtstate = Py_NewInterpreter();

PyRun_SimpleString( "import time\nwhile 1:\n\tprint \"I won't
stop\"\n\ttime.sleep(1.0)\n" );

printf( "Interpreter was shutdown ... yeah \n" );

PyEval_ReleaseLock();
}

int main(int argc, char *argv[] )
{
pthread_t thread;
PyThreadState *tstate;

Py_Initialize();
PyEval_InitThreads();
tstate = PyEval_SaveThread();

pthread_create( &thread, (pthread_attr_t *)NULL, mythread, NULL );

sleep(3);

/* Make that pesky script die */
printf( "die ... \n" );
PyEval_AcquireLock();

PyThreadState_Clear(gtstate);
//PyThreadState_Delete(gtstate); // Fatal Python error:
Py_EndInterpreter: not the last thread

PyThreadState_Swap(gtstate);
Py_EndInterpreter(gtstate);

PyEval_ReleaseLock();
sleep(3); // Segmentation fault
PyEval_AcquireThread(tstate);
Py_Finalize();

return(0);
}

Jul 18 '05 #1
1 2252
Hi,
I've googled my heart out and reread the threading API countless times.
Any suggest or hints would be greatly appreciated.


You might have used the wrong keywords - the subject of killable threads
comes up every month once or twice, and usually is discussed to some
length. There exist some solutions to this problem that use tracing [1] to
forcefully terminate a thread, but these of course only work if the source
of trouble is a sequence of statements, not a single blocking one.

I myself had a similar problem a few days ago, and started using fork() and
killing the subprocesses. Right after I implemented my little framework, I
found that QThread from qt had a terminate method. Fearing my work had been
uneccessary, I tried to use them - and found that not only my thread, but
the whole program died when terminating the thread. That was because of
omniorb beeing in an inconsistent state.

Maybe you don't experience this using your code, but it certainly proved
that the arguments for deprecating thread terminating methods in java and
obmitting them in python seem to be valid.

[
[1]http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/ace7c98ef31bbf2d/2af9df4b63a48cbc?q=python+killable+thread&_done=%2 Fgroups%3Fq%3Dpython+killable+thread%26hl%3Den%26l r%3D%26ie%3DUTF-8%26c2coff%3D1%26sa%3DN%26tab%3Dwg%26&_doneTitle=B ack+to+Search&&d#2af9df4b63a48cbc
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2499
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
0
1830
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
23
2928
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
1
537
by: Martin | last post by:
Greetings, I am new to python and wish to embed python in an 3D graphics application to provide application automation. The high level goal is to be able to drive my app from a script for batch...
1
3162
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
0
1315
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
0
1413
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
4
1665
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application running on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
3
2671
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.