Any idea of stopping the execution of PyRun_File()

B

BigHand

Hi,All
I have an embedded python application. which is a MFC app with
Python interpreter embedded.

In the App, I have a separate thread to execute a Python script
(using the PyRun_File), but if the user want to stop the executing
script, how should I do?

A possible way is terminate the thread of executing the scripts by
TerminateThread(). but TerminateThread() is unsafe and not
recommended.

guys, could you guide me on this?

B.R.
Harry
 
G

Gabriel Genellina

I have an embedded python application. which is a MFC app with
Python interpreter embedded.

In the App, I have a separate thread to execute a Python script
(using the PyRun_File), but if the user want to stop the executing
script, how should I do?

A possible way is terminate the thread of executing the scripts by
TerminateThread(). but TerminateThread() is unsafe and not
recommended.

guys, could you guide me on this?

You could use PyThreadState_SetAsyncExc (to "inject" an exception), or
perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a
KeyboardInterrupt). This should work fine for well-behaved scripts, but if
it ignores all exceptions like this:
try: ...
except: pass
you'll have to look at ceval.c how to break out of the running loop.

(this is yet another argument against indiscriminately using a bare except
clause...)
 
K

kongcheng1400

You could use PyThreadState_SetAsyncExc (to "inject" an exception), or  
perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a  
KeyboardInterrupt). This should work fine for well-behaved scripts, but if  
it ignores all exceptions like this:
        try: ...
        except: pass
you'll have to look at ceval.c how to break out of the running loop.

(this is yet another argument against indiscriminately using a bare except  
clause...)

Thanks Very much, You are always kindly helping me.
 
B

BigHand

You could use PyThreadState_SetAsyncExc (to "inject" an exception), or  
perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a  
KeyboardInterrupt). This should work fine for well-behaved scripts, but if  
it ignores all exceptions like this:
        try: ...
        except: pass
you'll have to look at ceval.c how to break out of the running loop.

(this is yet another argument against indiscriminately using a bare except  
clause...)

hello.Gabriel.
PyThreadState_SetAsyncExc cause me a win32 exception of "Stack
overflow"
msvcr80d.dll!_getptd_noexit() Line 592 C
msvcr80d.dll!_getptd() Line 658 + 0x5 bytes C
msvcr80d.dll!_LocaleUpdate::_LocaleUpdate(localeinfo_struct *
plocinfo=0x00000000) Line 264 + 0x5 bytes C++
msvcr80d.dll!_output_l(_iobuf * stream=0x10311d40, const char *
format=0x1e26e0ac, localeinfo_struct * plocinfo=0x00000000, char *
argptr=0x000333c4) Line 1028 C++
msvcr80d.dll!fprintf(_iobuf * str=0x10311d40, const char *
format=0x1e26e0ac, ...) Line 70 + 0x13 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2000
+ 0x19 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C
python30_d.dll!PyErr_Occurred() Line 133 + 0x5 bytes C
python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4) Line 2001
+ 0x5 bytes C
python30_d.dll!PyThreadState_Get() Line 349 + 0xa bytes C

My app is a MFC app, it have the main thread and the sub thread, the
sub thread run the script(Py_RunFile),
That I am looking for a way to stop the Py_RunFile from the main
thread.
that looks like I have to use the TerminateThread() .

thanks.Gabriel, could you find some example for me?
 
D

David Shapiro

Hello,

I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows:

try:
(opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
except getopt.GetoptError:
usage()

for opt in opts:
(key, value) = opt
if (key in ("-v", "--version")):
print "Version: " + version
sys.exit(1)
if (key in ("-h", "--help")):
usage()
if (key in ("-s", "--server")):
server = value
if (key in ("-p", "--port")):
port = value
if (key in ("-n", "--dsName")):
dsName = value
if (key in ("-j", "--jndiName")):
jndiName = value
if (key in ("-d", "--driverName")):
driverName = value
if (key in ("-l", "--driverURL")):
driverURL = value
if (key in ("-u", "--user")):
user = value
if (key in ("-c", "--passWD")):
passWD = value
if (key in ("-t", "--targetServer")):
targetServer = value
if (key in ("-q", "--testTableName")):
testTableName = value
if (key in ("-w", "--whereProp")):
whereProp = value


print "server: " + server
print "port: " + port
print "dsName: " + dsName
print "jndiName: " + jndiName
print "driverName: " + driverName
print "driverURL: " + driverURL
print "user: " + user
print "passWD: " + passWD
print "testtable: " + testTableName
print "targetServer: " + targetServer
print "whereProp: " + whereProp

The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE". It returns back just SQL. How do I get it to return the whole thing that is in double quotes? Another problem is that whereProp value is just not seen. Is there a limit to the size for argv?

If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way. A temporary workaround is to use " ".join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces. Also, it just seem weird I have to do a work around like that. I could have swore using double quotes should have fixed this issue, but they do not seem to work.

David
 
J

Javier Collado

Hello,

It's strange behaviour. Have you tried argparse
(http://code.google.com/p/argparse/)? I've been using it for long time
without any problem like that?

Best regards,
Javier

2009/6/10 David Shapiro said:
Hello,

I have been trying to find an example of how to deal with options that have spaces in them.  I am using jython, which is the same I think as python 2.2.3.   I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse).   The code is as follows:

   try:
       (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
   except getopt.GetoptError:
       usage()

   for opt in opts:
       (key, value) = opt
       if (key in ("-v", "--version")):
               print "Version: " + version
               sys.exit(1)
       if (key in ("-h", "--help")):
               usage()
       if (key in ("-s", "--server")):
               server = value
       if (key in ("-p", "--port")):
               port = value
       if (key in ("-n", "--dsName")):
               dsName = value
       if (key in ("-j", "--jndiName")):
               jndiName = value
       if (key in ("-d", "--driverName")):
               driverName = value
       if (key in ("-l", "--driverURL")):
               driverURL = value
       if (key in ("-u", "--user")):
               user = value
       if (key in ("-c", "--passWD")):
               passWD = value
       if (key in ("-t", "--targetServer")):
               targetServer = value
       if (key in ("-q", "--testTableName")):
               testTableName = value
       if (key in ("-w", "--whereProp")):
               whereProp = value


print "server: " + server
print "port: " + port
print "dsName: " + dsName
print "jndiName: " + jndiName
print "driverName: " + driverName
print "driverURL: " + driverURL
print "user: " + user
print "passWD: " + passWD
print "testtable: " + testTableName
print "targetServer: " + targetServer
print "whereProp: " + whereProp

The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE".  It returns back just SQL.  How do I get it to return the whole thing that is in double quotes?  Another problem is that whereProp value is just not seen. Is there a limit to the size for argv?

If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way.  A temporary workaround is to use " ".join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces.  Also, it just seem weird I have to do a work around like that.  I could have swore using double quotes should have fixed this issue, but they do not seem to work.

David
 
D

David Shapiro

Unfortunately, I had no luck installing argparse, which is really confusing because I would need to use some old version pre-optik to use I think. The Jython I use is like python 2.2.3. I spent all of yesterday trying to get either getopt, argparse, or optparse to work. Even with optparse I had to modify a module. For example, in textwrap.py, they have

@ line 124 in the module:

if self.replace_whitespace:
#if isinstance(text,str):
# text = text.translate(self.whitespace_trans)
#elif isinstances(text,Unicode):
text = text.translate(self.unicode_whitespace_trans)
return text

I had to comment out the if isinstance(text,str) and elif. Is the double quotes supposed to work?

-----Original Message-----
From: Javier Collado [mailto:[email protected]]
Sent: Wednesday, June 10, 2009 9:38 AM
To: David Shapiro
Cc: (e-mail address removed)
Subject: Re: getop or optparse with option with spaces?

Hello,

It's strange behaviour. Have you tried argparse
(http://code.google.com/p/argparse/)? I've been using it for long time
without any problem like that?

Best regards,
Javier

2009/6/10 David Shapiro said:
Hello,

I have been trying to find an example of how to deal with options that have spaces in them.  I am using jython, which is the same I think as python 2.2.3.   I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse).   The code is as follows:

   try:
       (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
   except getopt.GetoptError:
       usage()

   for opt in opts:
       (key, value) = opt
       if (key in ("-v", "--version")):
               print "Version: " + version
               sys.exit(1)
       if (key in ("-h", "--help")):
               usage()
       if (key in ("-s", "--server")):
               server = value
       if (key in ("-p", "--port")):
               port = value
       if (key in ("-n", "--dsName")):
               dsName = value
       if (key in ("-j", "--jndiName")):
               jndiName = value
       if (key in ("-d", "--driverName")):
               driverName = value
       if (key in ("-l", "--driverURL")):
               driverURL = value
       if (key in ("-u", "--user")):
               user = value
       if (key in ("-c", "--passWD")):
               passWD = value
       if (key in ("-t", "--targetServer")):
               targetServer = value
       if (key in ("-q", "--testTableName")):
               testTableName = value
       if (key in ("-w", "--whereProp")):
               whereProp = value


print "server: " + server
print "port: " + port
print "dsName: " + dsName
print "jndiName: " + jndiName
print "driverName: " + driverName
print "driverURL: " + driverURL
print "user: " + user
print "passWD: " + passWD
print "testtable: " + testTableName
print "targetServer: " + targetServer
print "whereProp: " + whereProp

The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE".  It returns back just SQL.  How do I get it to return the whole thing that is in double quotes?  Another problem is that whereProp value is just not seen. Is there a limit to the size for argv?

If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way.  A temporary workaround is to use " ".join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces.  Also, it just seem weird I have to do a work around like that.  I could have swore using double quotes should have fixed this issue, but they do not seem to work.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top