process cannot access the file because it is being used by otherprocess

S

shanti bhushan

Hi,

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.

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")

def invoke_server3():
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:\New2\ >YourOutput.txt"')
except OSError:
print "os error"
file.close()
sys.exc_clear()
os.remove("mongoose-2.8.exe")


def kill_server():
killret=subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"')
print killret


invoke_server1()
time.sleep(5)
kill_server()
time.sleep(5)
invoke_server2()
time.sleep(5)
kill_server()
time.sleep(5)
invoke_server3()
time.sleep(5)
kill_server()
 
T

Tim Golden

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
 
S

shanti bhushan

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

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 code only invokes the server but is not killing after 10 seconds.

my purpose is invoke server many times with different argument and
kill it.
but when ever i use subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /
c
"taskkill /F /IM mongoose-2.8.exe >YourOutput1.txt"'
this gives me error "process cannot access the file because it is
being used by other process"
 
C

Chris Rebert

On Mon, Jun 21, 2010 at 6:44 AM, Felipe Vinturini
Your problem seems to be with stdout redirect to the same file:
">YourOutput1.txt". Windows is not like Unix like systems!
You can try, instead of redirecting to the same file, redirect each to a
separate file and use the following form:
<code>
       proc = subprocess.Popen(r'C:\WINDOWS\system32\cmd.exe /C
"D:\372\pythonweb\mongoose-2.8.exe -root D:\New1\ 1>YourOutput.txt 2>&1"')
</code>

I believe the cmd.exe ugliness can also be avoided altogether:
proc = subprocess.Popen([r'D:\372\pythonweb\mongoose-2.8.exe',
'-root', 'D:\New1\'], stderr=subprocess.STDOUT,
stdout=file("YourOutput.txt", 'w'))

Cheers,
Chris
 
T

Thomas Jollans

[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
 

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,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top