Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #72529
| X-Received | by 10.182.125.4 with SMTP id mm4mr7438267obb.49.1401810190338; Tue, 03 Jun 2014 08:43:10 -0700 (PDT) |
|---|---|
| X-Received | by 10.140.32.166 with SMTP id h35mr27270qgh.23.1401810190303; Tue, 03 Jun 2014 08:43:10 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!h18no104020igc.0!news-out.google.com!s2ni2489qap.0!nntp.google.com!j5no839290qaq.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Tue, 3 Jun 2014 08:43:10 -0700 (PDT) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=208.71.232.130; posting-account=hsRhvwoAAAAf6DzmCC6SQI-1M5DZzNNx |
| NNTP-Posting-Host | 208.71.232.130 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <3c0be3a7-9d2d-4530-958b-13be97db3765@googlegroups.com> (permalink) |
| Subject | multiprocess (and paramiko) |
| From | mennis <michaelian.ennis@gmail.com> |
| Injection-Date | Tue, 03 Jun 2014 15:43:10 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| Xref | csiph.com comp.lang.python:72529 |
Show key headers only | View raw
I was able to work around this by using a completely different design but I still don''t understand why this doesn't work. It appears that the process that launches the process doesn't get access to updated object attributes. When I set and check them in the object itself it behaves as expected. When I check them from outside the object instance I get the initial values only. Could someone explain what I'm missing?
Here I have a simple multiprocessing class that when initializes takes a connected SSHClient instance and a command to run on the associated host in a new channel.
import multiprocessing
from time import time
from Crypto import Random
import paramiko
class Nonblock(multiprocessing.Process):
def __init__(self, connection, cmd):
Random.atfork()
multiprocessing.Process.__init__(self)
self.transport = connection.get_transport()
if self.transport is None:
raise ConnectionError("connection.get_transport() returned None ")
self.channel = self.transport.open_session()
self.command = cmd
self.done = False
self.stdin = None
self.stdout = None
self.stderr = None
self.status = None
self.message = str()
self.time = float()
def _read(self, channelobj):
"""read until EOF"""
buf = channelobj.readline()
output = str(buf)
while buf:
buf = channelobj.readline()
output += buf
return output
def run(self):
start = time()
stdin, stdout, stderr = self.channel.exec_command(command=self.command)
self.stderr = self._read(stderr)
self.status = stdout.channel.recv_exit_status()
if self.status != 0:
self.status = False
self.message = self.stderr
else:
self.status = True
self.message = self._read(stdout)
self.time = time() - start
stdin.close()
self.done = True
I expect to use it in the following manner:
from simplelib import Nonblock
from time import sleep
from paramiko import SSHClient, AutoAddPolicy
if __name__== "__main__":
connection = SSHClient()
connection.set_missing_host_key_policy(AutoAddPolicy())
username = "uname"
hostname = "hostname"
password = "password"
connection.connect(hostname, 22, username, password)
print connection.exec_command("sleep 1; echo test 0")[1].read()
n = Nonblock(connection,"sleep 20; echo test 2")
n.start()
print connection.exec_command("sleep 1; echo test 1")[1].read()
while not n.done:
sleep(1)
print n.message
print "done"
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
multiprocess (and paramiko) mennis <michaelian.ennis@gmail.com> - 2014-06-03 08:43 -0700
Re: multiprocess (and paramiko) Chris Angelico <rosuav@gmail.com> - 2014-06-04 02:08 +1000
Re: multiprocess (and paramiko) Roy Smith <roy@panix.com> - 2014-06-03 12:43 -0400
Re: multiprocess (and paramiko) mennis <michaelian.ennis@gmail.com> - 2014-06-03 11:29 -0700
csiph-web