A Puzzle with PySimpleApp

We have an application (with a gui) that
embeds Python inside it. Code between BEGIN PROGRAM and END PROGRAM is
executed by the Python processor, and each program causes a new thread to be
created, which dies at the END PROGRAM call. (Other code inbetween would be our
standard syntax.)

In order to minimize contention with the
regular gui, we create wx.PySimpleApp objects and destroy them as soon as
possible. This generally works fine, but the two programs below do not get
along well. Each one can be run many times in the same session, but running
the second one after the first one has been run causes the file dialog ShowModal
call always to return 5101, Cancel, without ever showing the dialog.

Putting all the wxPython stuff on a separate
thread does not help. My suspicion is that there is some state that is not removed
when the app is destroyed and the new thread requires it. But, to reiterate,
each program can be run repeatedly separately. Sometimes after running the
second one, running the first one again produces a “common dialog failed” alert
with a code of ffff.

This is all on Windows, and wxPython 2.8 (it
also failed on 2.6).

Any hints on what might be happening here or
how to get around it would be greatly appreciated.

*** running the
following as one program works. As separate programs, the second one fails.

BEGIN PROGRAM.

import spss,
spssdata, re, wx, sys

import threading

stringVars = []

for i in
range(spss.GetVariableCount()):

** if
spss.GetVariableType(i) != 0:**

** stringVars.append(spss.GetVariableName(i))**

pcvars = "not
set"

app =
wx.PySimpleApp()

dlg =
wx.MultiChoiceDialog(None, “Select one or more variables\nfor
analysis”,

** “Descriptive
Statistics”, stringVars)**

if dlg.ShowModal()
== wx.ID_OK:

** varsx =
dlg.GetSelections()**

else:

** varsx = None**

dlg.Destroy()

app.Destroy()

print varsx

END PROGRAM.

BEGIN PROGRAM.

import wx

repapp =
wx.PySimpleApp()

repdlg =
wx.FileDialog(None,“Save file as
…”,defaultDir="~/Desktop",defaultFile="",wildcard=“PDF
File (.pdf)|.pdf|Word File (.doc)|.doc|Powerpoint File
(.ppt)|.ppt”,style=wx.SAVE)

print
“repdlg:”, type(repdlg), repdlg

rc =
repdlg.ShowModal()

if rc== wx.ID_OK:

** outfile =
repdlg.GetPath()**

else:

** outfile = None**

repdlg.Destroy()

repapp.Destroy()

END PROGRAM.

Jon K. Peck

SPSS Inc.

peck@spss.com

312-651-3435

Peck, Jon wrote:

In order to minimize contention with the regular gui, we create wx.PySimpleApp objects and destroy them as soon as possible.

I'm pretty sure that wx does not support having more than one wxApp instance in a program, even if you destroy one before creating the next. It certainly doesn't support separate wxApps in separate threads.

What is your host app? is it a wxApp?

I think you'll need to figure out a way to create on wxApp instance for the duration of your program, and have all the other code use it. (you won't be able have gui stuff going on in different threads, either)

Could you use separate processes?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

We only need one wxApp at a time, and it typically just needs to put up a dialog box and then go away. Threading was an experiment to see if putting that one on a separate thread from the rest of the code might help, but it didn't. Leaving the app object around between programs and reusing it does not help.

The hosting app is a traditional Windows C++ app. The odd thing is that in many cases running a program that creates and destroys the app repeatedly or running several different programs that create and destroy the app works fine.

If we put the wxPython app in a separate process, it works fine, as one would expect, but the IPC is a bit more work and it forces the user to structure the program in a way that they might not want.

···

-----Original Message-----
From: Christopher Barker [mailto:Chris.Barker@noaa.gov]
Sent: Wednesday, October 03, 2007 11:08 AM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] A Puzzle with PySimpleApp

Peck, Jon wrote:

In order to minimize contention with the regular gui, we create
wx.PySimpleApp objects and destroy them as soon as possible.

I'm pretty sure that wx does not support having more than one wxApp
instance in a program, even if you destroy one before creating the next.
It certainly doesn't support separate wxApps in separate threads.

What is your host app? is it a wxApp?

I think you'll need to figure out a way to create on wxApp instance for
the duration of your program, and have all the other code use it. (you
won't be able have gui stuff going on in different threads, either)

Could you use separate processes?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Peck, Jon wrote:

The hosting app is a traditional Windows C++ app.

No GUI or mainloop?

The odd thing is that in many cases running a program that creates
and destroys the app repeatedly..

That is odd - but with a lot of this kin do of thing, something that is not supported might work once in a while, if you're lucky.

Leaving the app object around between programs and reusing it does not help.

OK -- I'd focus on this, as it should work. With some more detail, maybe we can figure out what the problem is.

However, I'm a bit confused by what "between programs" means. It sounded like you don't want to deal with IPC, so you must have only one program running. Are you calling multiple python scripts, each of which needs to pop up a dialog or something? if so, then that should work with one running wxApp. In fact, I think you can have a wxApp without a mainloop running, and still pop up dialogs, as they have their own mainloop.

One more note -- it is generally easier to have your main program by Python, and call C/C++ stuff, than the other way around. Could you re-structure to have a wxPython program as the host, and have your C/C++ routines called from there?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

See below.

···

-----Original Message-----
From: Christopher Barker [mailto:Chris.Barker@noaa.gov]
Sent: Wednesday, October 03, 2007 12:19 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] A Puzzle with PySimpleApp

Peck, Jon wrote:

The hosting app is a traditional Windows C++ app.

No GUI or mainloop?
[>>>Peck, Jon] It is a very large Windows app with a complex, multi-window gui. The conflict between the two guis is perhaps the biggest worry. It is what motivated shutting down the wxPython part as quickly as possible.

The odd thing is that in many cases running a program that creates
and destroys the app repeatedly..

That is odd - but with a lot of this kin do of thing, something that is
not supported might work once in a while, if you're lucky.
[>>>Peck, Jon] It actually works most of the time, which might just mean that we are exceptionally lucky.

Leaving the app object around between programs and reusing it does not help.

OK -- I'd focus on this, as it should work. With some more detail, maybe
we can figure out what the problem is.

However, I'm a bit confused by what "between programs" means. It sounded
like you don't want to deal with IPC, so you must have only one program
running. Are you calling multiple python scripts, each of which needs to
pop up a dialog or something? if so, then that should work with one
running wxApp. In fact, I think you can have a wxApp without a mainloop
running, and still pop up dialogs, as they have their own mainloop.
[>>>Peck, Jon]
[>>>Peck, Jon] It is all one "program". I was referring to the programs as in the syntax I posted. There is a stream of traditional SPSS syntax running that is interleaved with Python code between
BEGIN PROGRAM
and
END PROGRAM
statements. If the wxPython app lifetime is within one of these programs, all is well, and even running different programs each with its own wxPython app starting and ending usually works.

There is some evidence to suggest that using PySimpleApp makes the problem worse, but that isn't conclusive.

Each PROGRAM in the above sense is run on a new thread that dies at the end of the program, so any initialization related to the thread would need to be repeated for each program, most likely, We had some issues with COM in this mode that required adding code to reinitialize the Python COM connection each time a program starts. I rather suspect that there might be similar issues here.

One more note -- it is generally easier to have your main program by
Python, and call C/C++ stuff, than the other way around. Could you
re-structure to have a wxPython program as the host, and have your C/C++
routines called from there?
[>>>Peck, Jon] We are dealing with a legacy app with millions of lines of code, so we can't restructure it, but we do have an alternative way of running it in which there is no gui at all on our side and all the gui stuff comes from Python. In that mode, everything works fine, but in that mode there is only one program: no starting and stopping.

-Jon

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Peck, Jon wrote:

Each PROGRAM in the above sense is run on a new thread that dies at
the end of the program, so any initialization related to the thread
would need to be repeated for each program, most likely, We had some
issues with COM in this mode that required adding code to
reinitialize the Python COM connection each time a program starts. I
rather suspect that there might be similar issues here.

Have you tried keeping that thread around and reusing it for the next snippet of wxPython code?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Unfortunately I can't keep the Python thread alive between programs. I did putting PySimpleApp on a separate thread but it also dies between programs.

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Wednesday, October 03, 2007 3:53 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] A Puzzle with PySimpleApp

Peck, Jon wrote:

Each PROGRAM in the above sense is run on a new thread that dies at
the end of the program, so any initialization related to the thread
would need to be repeated for each program, most likely, We had some
issues with COM in this mode that required adding code to
reinitialize the Python COM connection each time a program starts. I
rather suspect that there might be similar issues here.

Have you tried keeping that thread around and reusing it for the next
snippet of wxPython code?

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org