Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get asynchronous input AND output using subprocess.POPEN

I've been working on this for a few hours and haven't been able to come up with a good solution. A little background, I'm running a password cracking program that's closed source from the command line but have to constantly pause it when my gpu temperature gets too hot.

I do other manipulations in python with this program so that's the language I'd prefer. Anyways, the password program gives periodic updates on how well it's doing, the gpu temperature, etc. and allows me to pause it at any time.

I'm getting the temperature fine but because of blocking issues I'm guessing I can't send the pause command. It's not doing anything at least. I've seen several examples of threading the output, but haven't seen something that that uses threading input and output without causing any issues.

I mean for all I know this could be impossible under current POPEN constraints but would appreciate some direction.

popen = Popen(command, stdout=PIPE, stdin=PIPE, shell=True)
lines_iterator = iter(popen.stdout.readline, b"")
while 1:
    for line in lines_iterator:
        cleanLine = line.replace("\n", "")
        p = re.compile('[0-9][0-9]c Temp')
        m = p.search(cleanLine)
        print cleanLine
        if m:
            temperature = m.group(0)
            if int(temperature[:2]) > 80:
                overheating = True
                print "overheating"
        if overheating:
            if "[s]tatus [p]ause [r]esume [b]ypass [q]uit" in line:

                #It's not doing anything right here, it just continues
                print popen.communicate("p")[0]

This is the gist of my code. It's still kind of through the hacky phase so I know that it might not be following best coding practices.

like image 246
themuffinman Avatar asked Sep 03 '25 07:09

themuffinman


1 Answers

EDIT: Sorry, I was confused in the initial answer about the scope of overheating. I deleted the first part of my answer since it's not relevant anymore.

communicate will wait for the process to exit so it might not be what you're looking for in this case. If you want the process to keep going you can use something like popen.stdin.write("p"). You might also need to send a "\n" along if that's required by your process.

Also, if you're OK with an extra dependency you might be interested in the pexpect module that was designed to control interactive processes.

like image 180
gbs Avatar answered Sep 04 '25 20:09

gbs