python: what's wrong with my subprocess.Popen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting python: what's wrong with my subprocess.Popen
# 1  
Old 12-18-2010
python: what's wrong with my subprocess.Popen

my script is
Code:
#!/usr/bin/env python
import datetime
import subprocess
import sys
import os
import signal
from time import sleep
def runForAWhile(cmd, secs=10):
 print("running %s" % cmd)
 timeout = datetime.timedelta(seconds=secs)
 print timeout
 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
 status = proc.poll()
 start = datetime.datetime.now()
 while (status is None and (datetime.datetime.now() - start) <
timeout): #not timed out
     #print proc.stdout.readline() #TODO timestamp?
 print status
     #print datetime.datetime.now() - start
 if 0 == status:
     print("'%s' is program exited" %cmd)
 else:
     try:
         os.kill(proc.pid, signal.SIGINT)
     print "Process timeout: '%s'" % cmd
     except OSError:
         pass
cmd="ping 128.224.165.20 -c 4"
runForAWhile(cmd,30)

Why "status' is aways !=0, ----> the program is NOT exit?

Lei

Last edited by yanglei_fage; 12-19-2010 at 01:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

[Python] - subprocess issue

So I have this basic script, see below import subprocess import shlex command = "gcloud projects list" subprocess.check_output(shlex.split(command)) subprocess.check_call(shlex.split(command)) The subprocess.check_call(shlex.split(command)) actually return what I expect. It returns... (6 Replies)
Discussion started by: scj2012
6 Replies

2. Programming

What is wrong with below python inheritance code?

I am using python 3.4. Below is the exception I am getting- Traceback (most recent call last): File "./oop.py", line 20, in <module> y = DerivedClass("Manu") File "./oop.py", line 15, in __init__ super().__init__(self,value) TypeError: __init__() takes 2 positional arguments but... (2 Replies)
Discussion started by: Tanu
2 Replies

3. Shell Programming and Scripting

Subprocess.popen() should write to log without waiting for process to complete

Here is my test code process = sp.Popen( + , bufsize=1, universal_newlines=True, stdout=sp.PIPE, stderr=sp.STDOUT, cwd=src_home) output, _ =... (2 Replies)
Discussion started by: ezee
2 Replies

4. Shell Programming and Scripting

Python - Function print vs return - whats wrong

All, I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this #!/usr/bin/python def div4and6(s,e): for i in range(s,e+1): if... (5 Replies)
Discussion started by: oky
5 Replies

5. Shell Programming and Scripting

Python subprocess

Hi guys, I'm learning python and perl and i was trying to run from python a perl script using the subprocess module. I have an issue that i don't understand regarding this. I run this code: #!/usr/bin/python import subprocess p2 = subprocess.Popen(,stdout=subprocess.PIPE) output2 =... (2 Replies)
Discussion started by: capitanui
2 Replies

6. Shell Programming and Scripting

Xterm using python subprocess

Hi, I am trying to run a shell script using subprocess in python. I can run simple script with arguments using subprocess.But I am not able to embed xterm in subrocess command. #!/usr/bin/python import subprocess subprocess.call() Above code gives me error. Please help me in... (2 Replies)
Discussion started by: diehard
2 Replies

7. Shell Programming and Scripting

Python subprocess module

I need to run this command using python subprocess module (notice I'm using only variables): cmd = TESTPATH + ' -s ' + serviceName + ' -r ' + rdir + \ ' -m ' + masterAcct + ' -p ' + persona + ' -P ' + passwd (3 Replies)
Discussion started by: erick_tuk
3 Replies

8. AIX

Subprocess errors

Hi Guys, Just a question about subprocesses.. Lately one of our servers has started to throw out the following error: SYSTEM ERROR: Too many subprocesses, cannot fork. Errno=12 We've already increased the threshold twice. Its now up to 8000 and the swap space has also been increased. We... (6 Replies)
Discussion started by: Jazmania
6 Replies

9. Shell Programming and Scripting

Doubt about pipes and subprocess

Hi, I am having a trivial doubt. Please see the below pipeline code sequence. command1 | (command 2; commend 3) I am aware that the command that follows pipe will run in the sub shell by the Unix kernel. But how about here? Since these set of commands are grouped under "parantheses", will... (6 Replies)
Discussion started by: royalibrahim
6 Replies

10. Shell Programming and Scripting

Python: popen problems

Hello I'm writing a web server in python(obelisk-http.sourceforge.net) and I'm having a greeat problem with POST method it like that When someone make a POST request to the server it must open the executable(perl/python/.exe/elf) and send to the STANDART in (stdin) the request and get the... (2 Replies)
Discussion started by: sendai
2 Replies
Login or Register to Ask a Question
lfc_python(3)							 Python Reference						     lfc_python(3)

NAME
lfc - Python interface to the LFC lfcthr - Thread enabled version of Python interface to the LFC SYNOPSIS
import lfc import lfcthr lfcthr.init() DESCRIPTION
The lfc module permits you to access the LFC client interface from python programs. The lfc module is a swig wrapping of the standard C interface. For detailed descriptions of each function see the individual man page of each function. The lfcthr module is a version of the lfc module supporting multi-threaded Python clients. Its usage is similar to the usage of the lfc module except the obligatory initialisation call lfcthr.init() in the main program before threads are started. There follows a series of examples of how to use selected functions and how to retrieve the information returned by them: Examples are finding the GUID of an existing entry, listing the replicas of a given GUID and setting and retrieving the comment associated with an entry. EXAMPLE
#!/usr/bin/python import sys import lfc """ # stat an existing entry in the LFC and print the GUID """ name = "/grid/dteam/my.test" stat = lfc.lfc_filestatg() res = lfc.lfc_statg(name,"",stat) if res == 0: guid = stat.guid print "The GUID for " + name + " is " + guid else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # list the replicas of a given entry, starting from the GUID """ guid = "6a3164e0-a4d7-4abe-9f76-e3b8882735d1" listp = lfc.lfc_list() flag = lfc.CNS_LIST_BEGIN print "Listing replicas for GUID " + guid num_replicas=0 while(1): res = lfc.lfc_listreplica("",guid,flag,listp) flag = lfc.CNS_LIST_CONTINUE if res == None: break else: rep_name = res.sfn print "Replica: " + rep_name num_replicas = num_replicas + 1 lfc.lfc_listreplica("",guid,lfc.CNS_LIST_END,listp) print "Found " + str(num_replicas) + " replica(s)" EXAMPLE
#!/usr/bin/python import sys import lfc import re """ # setting and retrieving a comment on a file """ file = "/grid/dteam/my.test" comment = "MyComment" res = lfc.lfc_setcomment(file,comment) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while setting comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) buffer="" for i in range(0,lfc.CA_MAXCOMMENTLEN+1): buffer=buffer + " " res = lfc.lfc_getcomment(file,buffer) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while reading the comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) r = re.compile("(.*?) ", re.DOTALL) comment = r.findall(buffer)[0] print "Read back comment " + comment EXAMPLE
#!/usr/bin/python """ # Using the lfc_readdirxr method """ import sys import lfc name = "/grid/dteam/my.test" dir = lfc.lfc_opendirg(name,"") if (dir == None) or (dir == 0): err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) while 1: read_pt = lfc.lfc_readdirxr(dir,"") if (read_pt == None) or (read_pt == 0): break entry, list = read_pt print entry.d_name try: for i in range(len(list)): print " ==> %s" % list[i].sfn except TypeError, x: print " ==> None" lfc.lfc_closedir(dir) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getlinks method """ result, list = lfc.lfc_getlinks("/grid/dteam/antotests/extratests/dir2/f105", "") print result print len(list) if (result == 0): for i in list: print i.path EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getreplica method """ result, list = lfc.lfc_getreplica("/grid/dteam/antotests/extratests/dir2/f105", "", "") print result print len(list) if (result == 0): for i in list: print i.host print i.sfn EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to add a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) print nentries print len(acls_list) for i in acls_list: print i.a_type print i.a_id print i.a_perm # When adding a first ACL for a given user, you also need to add the mask # When adding the second user ACL, it is not necessary anymore acl_user = lfc.lfc_acl() acl_mask = lfc.lfc_acl() acl_user.a_type=2 # 2 corresponds to CNS_ACL_USER acl_user.a_id=18701 # user id acl_user.a_perm=5 acl_mask.a_type=5 # 5 corresponds to CNS_ACL_MASK acl_mask.a_id=0 # no user id specified acl_mask.a_perm=5 acls_list.append(acl_user) acls_list.append(acl_mask) res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to remove a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) # Note : you cannot remove the owner ACL (i.e. for CNS_ACL_USER_OBJ type) if ACLs # ====== for other users exist. Ff all the other user ACLs are deleted, the owner # ====== ACL is automatically removed. for i in acls_list: print i.a_type print i.a_id print i.a_perm del acls_list[1] # delete a given user ACL from the list of ACLs res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/env python import lfcthr import os from threading import Thread class slave(Thread): def __init__ (self): Thread.__init__(self) def run(self): .... result = lfcthr.lfc_getreplica("", guid, "") .... return if __name__ == '__main__': os.environ['LFC_HOST'] = 'my_lfc.cern.ch' # Threaded library initialisation lfcthr.init() .... # Start up of threads for i in xrange(totalNumberOfSlaves): slv = slave(i) slv.start() .... EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getusrmap method """ result, list = lfc.lfc_getusrmap() print result print len(list) if (result == 0): for i in list: print i.userid + " " + i.username EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getgrpmap method """ result, list = lfc.lfc_getgrpmap() print result print len(list) if (result == 0): for i in list: print i.gid + " " + i.groupname KNOWN BUGS
The current interface to the lfc_getcomment(3), lfc_getcwd(3), lfc_readlink(3), lfc_seterrbuf(3) requires the passing of str object which is modified to contain the result (in a similar way to the C functions, which accept a buffer). However this breaks the immutability of python str. This will be changed in the future. SEE ALSO
LFC C interface man pages LFC
$Date: 2010-02-04 13:08:39 +0100 (Thu, 04 Feb 2010) $ lfc_python(3)