Trying to work around the lack of pexpect for native windows python I
had a play with os.popen2 and some threads. This looks promising
enough for what I want to do, but I hit on a problem with getting the
script to exit cleanly. Tried replacing popen with subprocess but I am
still confused. Either it needs to be killed from the task manager, or
I get a variety of different complaints and popping up "application
error" windows. I managed to get it to stop with os.abort(), but this
invites a complaint from windows about terminating in an unusual way.
Closing the file descriptors of the child process leads to a crash and
I think I should be sending a kill to the process, although I don't
know how (please tell me).
Would any of you care to have a look at the code below and make
suggestions for tidying it up properly? It also dies nastily if the
child process terminates. Eventually I would like to fire off a
program from a gui, supply input programmatically, depending upon the
unpredicable questions the program might ask, and prompt the user if
the child process asks something unknown. Also process the output into
a graphical form so the user can change options and run the child
again. Makes the crashing a bit of a problem! Entirely different
approaches which can accomplish this task would also be very very
welcome, I tried a telnet to localhost suggestion, but this was
painfully slow on this windows xp machine (using "services for unix"
telnet server). The communicate bit of subprocess only gives me one
chance to talk to the process. Should I be inheriting from subprocess
and editing the communicate method there?
Having googled extensively on this problem it seems that tidying up
this code could be useful to others too?
Thanks!!
Jon
---
import os,sys,time,subprocess
from threading import Thread
out = ""
class reader(Thread):
def __init__(self,file_to_read):
self.file_to_read=file_to_read
Thread.__init__(self)
def run(self):
global out
while 1:
i=self.file_to_read.read(1)
out+=i
sys.stdout.write(i) # for debugging
class writer(Thread):
def __init__(self,file_to_write):
self.file_to_write=file_to_write
Thread.__init__(self)
def run(self):
global out
while 1:
i=sys.stdin.read(1) # for debugging
out+=i
self.file_to_write.write(i)
if __name__=="__main__":
child = subprocess.Popen(sys.argv[1],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
# or: child.stdout,child.stdin = os.popen2(sys.argv[1])
r = reader(child.stdout)
w = writer(child.stdin)
r.start()
w.start()
while r.isAlive() and w.isAlive():
try:
time.sleep(10)
except:
print "\n=== in out now ===\n",out,"\n======"
break
print r.isAlive(),w.isAlive() # Who died, in or out??
time.sleep(1)
print r.isAlive(),w.isAlive() # Who died, in or out??
print "You fell off of the bottom of the script" 2 3326 jo*************@gmail.com (Jon Wright) wrote in message news:<56**************************@posting.google. com>... Trying to work around the lack of pexpect for native windows python I had a play with os.popen2 and some threads. This looks promising enough for what I want to do, but I hit on a problem with getting the script to exit cleanly. Tried replacing popen with subprocess but I am still confused. Either it needs to be killed from the task manager, or I get a variety of different complaints and popping up "application error" windows. I managed to get it to stop with os.abort(), but this invites a complaint from windows about terminating in an unusual way. Closing the file descriptors of the child process leads to a crash and I think I should be sending a kill to the process, although I don't know how (please tell me).
Would any of you care to have a look at the code below and make suggestions for tidying it up properly? It also dies nastily if the child process terminates. Eventually I would like to fire off a program from a gui, supply input programmatically, depending upon the unpredicable questions the program might ask, and prompt the user if the child process asks something unknown. Also process the output into a graphical form so the user can change options and run the child again. Makes the crashing a bit of a problem! Entirely different approaches which can accomplish this task would also be very very welcome, I tried a telnet to localhost suggestion, but this was painfully slow on this windows xp machine (using "services for unix" telnet server). The communicate bit of subprocess only gives me one chance to talk to the process. Should I be inheriting from subprocess and editing the communicate method there?
Having googled extensively on this problem it seems that tidying up this code could be useful to others too?
Thanks!!
Jon --- import os,sys,time,subprocess from threading import Thread
out = ""
class reader(Thread): def __init__(self,file_to_read): self.file_to_read=file_to_read Thread.__init__(self) def run(self): global out while 1: i=self.file_to_read.read(1) out+=i sys.stdout.write(i) # for debugging
class writer(Thread): def __init__(self,file_to_write): self.file_to_write=file_to_write Thread.__init__(self) def run(self): global out while 1: i=sys.stdin.read(1) # for debugging out+=i self.file_to_write.write(i)
if __name__=="__main__": child = subprocess.Popen(sys.argv[1], stdin=subprocess.PIPE, stdout=subprocess.PIPE) # or: child.stdout,child.stdin = os.popen2(sys.argv[1]) r = reader(child.stdout) w = writer(child.stdin) r.start() w.start() while r.isAlive() and w.isAlive(): try: time.sleep(10) except: print "\n=== in out now ===\n",out,"\n======" break print r.isAlive(),w.isAlive() # Who died, in or out?? time.sleep(1) print r.isAlive(),w.isAlive() # Who died, in or out?? print "You fell off of the bottom of the script"
Just curiosity: why did you try using UNIX methods in win32?
To prove that windows isn't unix? el*******@hotmail.com (Elbert Lev) wrote in message jo*************@gmail.com (Jon Wright) wrote in message
Trying to work around the lack of pexpect for native windows python I had a play with os.popen2 and some threads. This looks promising Just curiosity: why did you try using UNIX methods in win32?
I am after a particular functionality - I don't care what methods are
used; I just want to save myself repetitively typing crap into a
program and only answer the interesting questions it asks. Starting
another program and communicating programmatically via stdin and
stdout (usually defined in a language specification) is a convenience
for any multitasking os. Your suggestion helps a great deal however -
one just needed to know the translation into win32 for 'kill'. In my
case this seems sufficient:
def kill_win32(child):
import win32api, win32con
child.stdin.close() # should really check if this is open etc
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE,
0,[child.pid])
win32api.CloseHandle(handle)
child.stdout.close() # ditto
# error handling
Doubtless other children would resist this and some stronger magic
could be needed to snuff them out?
Perhaps this would be a useful thing to put into the "subprocess.py"
module which I think has now become part of the standard library...?
Probably a windows expert can do much better, and eventually lines
saying if win32 do this, if unix do that. Does that need a PEP or a
patch or is someone going to tell me why it shouldn't be there? Maybe
calling it "child.try_to_kill()" is enough to indicate that it won't
always work?
To prove that windows isn't unix?
Thanks for your comment. Highly motivating. I now have roughly the
functionality I wanted on both platforms. You hint at the idea that
there is a way to achieve the goal via "win32 methods". I am still
extremely curious to know what these methods might be, I only found
stories that they don't exist in the general case of win32 console io.
Since cygwin python has pty/pexpect modules and runs under win32 the
"proof" fails by contradication?
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: djw |
last post by:
Hi, Folks-
I have a question regarding the "proper" use of try: finally:... Consider
some code like this:
d = Device.open()
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise...
|
by: David Sworder |
last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to
post to this "general" group... sorry about that. It just occured to me
after my first post that the "general" group readers...
|
by: Dr. J |
last post by:
How to terminate a blocked thread?
In my form's "load" I launch a TCP listening thread that stays in an
infinite loop waiting for incoming TCP packets. In this form's "closing" I
try to...
|
by: roger beniot |
last post by:
I have a program that launches multiple threads with a ThreadStart
method like the following (using System.Net.Sockets.Socket for UDP
packet transfers to a server):
ThreadStart pseudo code:
...
|
by: Boniek |
last post by:
Hi
My main thread is waiting for data from database ( opened Sqlconnection etc)
and my Main Form is also blocked. I have a ProgressBar on Status Bar which
show to a user how percent data is...
| |
by: Andrew Baker |
last post by:
OK this has me perplexed, puzzled and bamboozled!
I have a remoting service which I displayed a message box in. I then
wondered what would happen if a client made a call to the service
while the...
|
by: Paul |
last post by:
Hi,
How do I wait until a thread is finished his job then continue to the
original thread?
public void main(string args)
{
Thread t = new Thread(new ThreadStart(DoWork));
t.Start();
|
by: Bill Davidson |
last post by:
Hello All:
I've got a question about synchronization requiremements in a C# worker
thread procedure that, among other things, sinks events from outside
sources. I realize the worker thread will...
|
by: Joe |
last post by:
Does anyone know the difference, in practical terms, between
Thread.Sleep (10000) and Thread.CurrentThread.Join (10000)??
The MSDN says that with Join, standard COM and SendMessage pumping...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |