Skip to content

run multiple commands in parallel - fastapi and npm for example #71

@FabienArcellier

Description

@FabienArcellier

The contextmanager alfred.parallel allow a developper to run multiple commands simulatneously and in a single terminal. It's useful to run fullstack apps, for example to run npm, fastapi and a database in same time.

When the user interrupts a command with CTRL+C, all commands that are currently executing are interrupted. If it is another signal as sigquit, it is forwarded to the commands being executed by alfred.

Log messages are prefixed with the command name in square brackets like in docker-compose to be able to track what is happening.

[front] 2022-12-01 12:56:54 hello world

usage

The developer declare the commands to be executed simultaneously in an alfred.parallel context block. Each of the commands will run independently of each other.

with alfred.parallel() as p:
  p.run(npm, ["run"], name="front")
  p.run(python, ["run"], name="back")

The commands are waited for indefinitely before exiting the alfred.parallel block. It is possible to disable this behavior and explicitly check the end of a command by setting the attribute wait to False. The instruction wait allow to wait the end of a command explicitely.

with alfred.parallel(wait=False) as p:
  p.run(npm, ["run"], name="front")
  p.run(python, ["run"], name="back")
  p.wait(commands=['front', 'back'])

alfred commands can be invoked in an alfred.parallel block.

with alfred.parallel() as p:
  p.invoke_command("frontend:run", name="front")
  p.invoke_command("backend:run", args={'verbose': verbose}, "back")

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions