[lxc-users] [python-lxc] Store attach_wait in variable
Benoit Barthelet
benoit.barthelet at gmail.com
Mon Apr 24 13:31:48 UTC 2017
I wrote this little snippet, it might give you an idea of how I "got
around" it...
import lxc
c = lxc.Container('tmp_container')
import tempfile
def run(container, command):
with tempfile.NamedTemporaryFile() as t:
container.attach_wait(lxc.attach_run_command, command,
stdout=t.file)
t.seek(0)
output = t.readlines()
print(output)
return output
hostname = run(c, ['cat', '/etc/hostname'])
[b'tmp_container\n']
id = run(c, ['id'])
[b'uid=0(root) gid=0(root) groups=0(root)\n']
On Wed, Mar 22, 2017 at 6:28 PM, Stéphane Graber <stgraber at ubuntu.com>
wrote:
> On Wed, Mar 08, 2017 at 10:08:29PM +0000, Élie Deloumeau-Prigent wrote:
> > Hello everyone,
> >
> > Is there any way to store attach_wait output into a variable?
> >
> > Thanks in advance!
> >
> > Élie.
>
> There is, but it's not exactly trivial. attach_wait spawns a subprocess
> through fork(), so the python process memory space isn't shared and the
> output therefore goes to stdout/stderr.
>
> What we did in the past for lxc-ls which needed something like that was
> have the main python process create a temporary file with
> tempfile.mkstemp(), then pass the fd as an argument to the function run
> by attach_wait, have the function redirect the output towards that fd
> and finally having the main process rewind and read from the fd after
> attach_wait returns.
>
>
> --
> Stéphane Graber
> Ubuntu developer
> http://www.ubuntu.com
>
> _______________________________________________
> lxc-users mailing list
> lxc-users at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-users
>
--
benoit barthelet
http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20170424/88f3d0e8/attachment.html>
More information about the lxc-users
mailing list