473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Having multiple instances of a single application start a singleinstance of another one

I have two applications that should work together, let's call them A and B.

The first time A starts, it should open a B process and start
communicating with it. All other times an A instance starts it should
simply talk with the B that already is open.

The problem here is, if I start say 40 A applications at once... how do
I check if a B is open "fast enough" so that the other A's (not the
first one) won't spawn new B's?

Im programming this in windows and am currently using the horrible
solution in A

if not win32gui.FindWindow(None, "Name of B"):
spawn_B_here()

This only works well if there is a small time between the A's started
first...

What would the best solution for my problem be?

/buffis
Feb 23 '07 #1
6 1510
The first time A starts, it should open a B process and start
communicating with it. All other times an A instance starts it should
simply talk with the B that already is open.
B should write its process id to a location known by both
applications. When A starts, it should read that PID from the file
and attempt to communicate with the process having that PID.

When B starts, it should also check for the file. If it's found and
if the PID in it is present in the process table, then B should exit.
Otherwise, it should start normally and write its own PID to the file.
Feb 23 '07 #2
Troy Melhase wrote:
>The first time A starts, it should open a B process and start
communicating with it. All other times an A instance starts it should
simply talk with the B that already is open.

B should write its process id to a location known by both
applications. When A starts, it should read that PID from the file
and attempt to communicate with the process having that PID.

When B starts, it should also check for the file. If it's found and
if the PID in it is present in the process table, then B should exit.
Otherwise, it should start normally and write its own PID to the file.
Three very simple questions then.

1. How do I find out a running applications process ID

2. How do I check if a process ID is bound to a running application.

3. There won't be any issues with different applications trying to read
and write the same file doing this?

/buffis
Feb 23 '07 #3
Three very simple questions then.
>
1. How do I find out a running applications process ID
import os
mypid = os.getpid()
2. How do I check if a process ID is bound to a running application.
this is os-specific. i'm sure there's a windows api provided for it.
3. There won't be any issues with different applications trying to read
and write the same file doing this?
shouldn't be any problem: only the first app started will write to the file.
Feb 23 '07 #4
On 2/23/07, buffinator <bu********@mymail.comwrote:
2. How do I check if a process ID is bound to a running application.
Under windows, this should work:
>>import win32process
running_pids = win32process.EnumProcesses()
running_pids
(0, 4, 1048, 1496, 1656, 1836, 1896, 756, 988, 1712, 220, 1156, 240,
1600, 1932, 428, 1100, 1880, 1152, 1444, 296, 624, 536, 412, 1812,
3384, 2064, 2164, 2344, 2516, 2816, 2992, 3412, 3628, 2836, 3168,
3420, 3408, 816, 1676, 504, 3244, 2404, 452, 1624, 3924, 2660, 3736,
3608, 1304)

--
Jerry
Feb 23 '07 #5
buffinator wrote:
I have two applications that should work together, let's call them A and B.

The first time A starts, it should open a B process and start
communicating with it. All other times an A instance starts it should
simply talk with the B that already is open.

The problem here is, if I start say 40 A applications at once... how do
I check if a B is open "fast enough" so that the other A's (not the
first one) won't spawn new B's?

Im programming this in windows and am currently using the horrible
solution in A

if not win32gui.FindWindow(None, "Name of B"):
spawn_B_here()

This only works well if there is a small time between the A's started
first...

What would the best solution for my problem be?

/buffis
Others have answered your specific question on the forum, I thought
I would make a suggestion. You should consider writing application B
as a Windows Service that gets started once (maybe even auto start
when the machine boots) and runs forever in the background. Then have
all your A's bind to it and communication (via sockets, pipes, filesystem,
database, ...). That way your A's won't have the problem you describe.

Just a suggestion.

-Larry
Feb 23 '07 #6
On Fri, 23 Feb 2007 23:39:03 +0100, Troy Melhase <tr**********@gmail.com>
wrote:
>The first time A starts, it should open a B process and start
communicating with it. All other times an A instance starts it should
simply talk with the B that already is open.

B should write its process id to a location known by both
applications. When A starts, it should read that PID from the file
and attempt to communicate with the process having that PID.

When B starts, it should also check for the file. If it's found and
if the PID in it is present in the process table, then B should exit.
Otherwise, it should start normally and write its own PID to the file.
You have a race condition here: if another instance of B is created
between the check and the creation of the file, you'll have two instances
running. And remember Murphy's law: yes, it will happen, and quicker than
you can imagine ;-)

To prevent this from happening, I usually create a special directory for
the PID file, and I delete both the file and the directory when the
process exits. The advantage is that os.mkdir is basically a "test and
set" in a single operation: if two of these are attempted at "the same
time", only one can succeed. It is also cross-platform and even works on
shared file systems.

So the code would be something like:

## Try to create directory
try:
os.mkdir(os.path.join(common_directory, "B_lock"))
except OSError:
## Failed: another instance is running
sys.exit()
## Create the PID file
# (...)
try:
## Application code
# (...)
finally:
## Remove the PID file
# (...)
## Delete directory
os.rmdir(os.path.join(common_directory, "B_lock"))

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
Feb 26 '07 #7

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

Similar topics

2
by: bmatt | last post by:
I am trying to support multiple interpreter instances within a single main application thread. The reason I would like separate interpreters is because objects in my system can be extended with...
4
by: logicalfeline | last post by:
How do you prevent multiple instances of a program from running? Cat
0
by: RonNanko | last post by:
Hi, let me first explain what my problem is all about: I have a third-party application, which does not allow multiple instances of itself. As I need to run the application in multiple instances...
3
by: SL | last post by:
All, As I understand it, a single application (i.e. IIS virtual directory) in ASP.NET may in fact have more than one corresponding HttpApplicationState object (more or less one per server...
3
by: Michel | last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for the user; the user open my app, do some works and try to open another instance of my app, I whant to show a message to...
3
by: hpark2_1999 | last post by:
Hi! I know how easy it is to create an NT services using .net framework (using Visual Studio 2003). Here is my problem. I came across a situation where I would like to run 2 instances of a...
0
by: rbg | last post by:
Have a web application which uses Data Cache. I need to understand what happens when a new instance of the same web application is created for for serving concurrent clients. What happens when...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
10
by: Peter Morris | last post by:
Some time ago I needed to write a single-instance app. That's an easy task but I also needed duplicate instances to pass their command line parameters to the first instance so that it can act upon...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.