From: Tim Golden on
On 21/06/2010 09:23, shanti bhushan wrote:
> i am using below code ,it works fine on ordinary python 26 ,but when i
> use this script in my python testing tool it gives me message "process
> cannot access the file because it is being used by other process" for
> the second time invoking of mongoose server.
> Please help me in handling this exception.

Before I make any suggestions on the code, I might suggest that you
learn to wait a little. You sent three pretty much identical messages
within the space of three hours. Believe me: if people aren't helping,
it's not because they haven't seen your first message. Or the follow-up.
Or the one after that. It's because they don't know the answer, or
haven't the time to answer. Or aren't in the same timezone as you and
so haven't woken up yet!

> def invoke_server2():
> file = open("mongoose-2.8.exe", "r")
> try:
> proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> \372\pythonweb\mongoose-2.8.exe -root D:\New1\>YourOutput.txt"')
> except OSError:
> print "os error"
> file.close()
> sys.exc_clear()
> os.remove("mongoose-2.8.exe")

OK. I'm not sure what you're achieving with the open ("mongoose...") line
and its corresponding close. In fact, I can't work out what the whole
exception block is achieving. I actually had to go and look up what
sys.exc_clear is doing -- and I don't think it's doing what you think
it's doing. You appear to be trapping an OS error, such as file-not-found
or access-denied, by trying to ignore the error and then deleting the
server
itself!

Let's straighten some stuff out. First your Popen line could almost
certainly
be simplified to this:

<code>
import subprocess

with open ("YourOutput.txt", "w") as outf:
proc = subprocess.Popen (
[r"D:\372\pythonweb\mongoose-2.8.exe", "-root", r"D:\New1"],
stdout=outf
)

</code>

and to kill the proc, you can just call proc.kill ()


Does that take you forward? Are you still seeing the "Cannot access file..."
errors?

TJG
From: Thomas Jollans on
On 06/21/2010 12:18 PM, shanti bhushan wrote:
> [snip]
>
> i used below code
>
> import subprocess
> import time
> def invoke_server1():
> proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C "D:
> \372\pythonweb\mongoose-2.8.exe -root D:\New1\"')
>
>
> invoke_server1()
>
>
> time.sleep(10)
> proc.kill()

This cannot work, since proc is not a global variable, it's local to
invoke_server1. You could do something like this instead:

def invoke_server1()
return subprocess.Popen([r'D:\372\pythonweb\mongoose-2.8.exe',
'-root', r'D:\New1\"])

server1_proc = invoke_server1()
time.sleep(10)
server1_proc.kill()


Are you running the code from a command prompt ? It should have printed
a nice and helpful NameError. Had you read that error message, it should
have been easy to figure out that there's something wrong with your
variable scopes.

-- Thomas