0

I'm trying to create an async subprocess and pass a variable to stdin but trio.run_process and trio.open_process both give an attribute error. It doesn't specify what is causing the error.

Traceback (most recent call last):
  File "./update.py", line 122, in pkgUpdates
    trio.run(self.asetup, password)
  File "/home/user/.local/lib/python3.7/site-packages/trio/_core/_run.py", line 1444, in run
    raise runner.main_task_outcome.error
  File "./update.py", line 65, in asetup
    nursery.start_soon(self.upProc, password, 'update')
  File "/home/user/.local/lib/python3.7/site-packages/trio/_core/_run.py", line 506, in __aexit__
    raise combined_error_from_nursery
  File "./update.py", line 75, in upProc
    await trio.open_process(['sudo', '-S', 'apt-get', cmd, '-y'],
  File "/home/user/.local/lib/python3.7/site-packages/trio/_deprecate.py", line 125, in __getattr__
    raise AttributeError(name)
AttributeError: open_process

I've also tried using trio.Process with stdin=io.BytesIO(password) but this gives an io.UnsupportedOperation error. Passing only bytes also gives an error.

The function is:

async def upProc(self, password, cmd):
    await trio.open_process(['sudo', '-S', 'apt-get', cmd, '-y'],
                   stdin=password.encode(), capture_stdout=True)

    if (cmd == 'update'):
        await self.upProc(password, 'upgrade')
    return
Hussar
  • 53
  • 5

1 Answers1

1

The reason you're getting an AttributeError on open_process and run_process is that they were only added in Trio v0.12.0, and you're probably using v0.11.0. This isn't your fault – I only released v0.12.0 about 20 minutes ago :-). But I guess you were looking at the docs for the development version, and that threw you off.

Assuming you can upgrade to v0.12.0, I think you want:

await trio.run_process(['sudo', '-S', 'apt-get', cmd, '-y'], stdin=password_bytes)

This starts the process, waits for it to complete, and then returns some information about the process, all in one shot.

Nathaniel J. Smith
  • 9,038
  • 4
  • 35
  • 46
  • Thanks! The functions are working now but it seams run_process is blocking the thread and I need to keep the gui responsive. open_process hangs forever and Process.stdout.receive_some() blocks the thread indefinitely . Maybe there's something I missed in the docs? – Hussar Aug 01 '19 at 22:39
  • What are you using for a gui? Generally you have to use special tricks to integrate gui libraries with async libraries like trio, because otherwise the two libraries fight over who should be controlling the thread. It's a big topic, not something that can be covered in a comment :-). Maybe drop by our chat, or ask a new question? – Nathaniel J. Smith Aug 02 '19 at 23:09