1

I am trying to build a one-button Darkice streaming box, I'm using a Pi 3 B+ with Stretch from NOOBS. I have everything set up but my Python script hangs up as soon as the button is pressed and Darkice runs, using process.call(Darkice). I think this is because it runs Darkice in the foreground, as when one types 'Darkice' in terminal that window becomes 'frozen' until ctrl-c is pressed. How do I have the 'Darkice' command run in my Python script so that Darkice runs in the background and the Python script continues beyond starting Darkice? Apologies if this is super-basic, I'm not a coder.

  • Not to take anything away from @computerjan's answer, but you should read this Q&A also: How to start a background process in Python? – Seamus Dec 28 '18 at 7:34
  • As an aside, your question seems to be more of a programming question than a Raspberry Pi question. While the boundaries are a wee bit fuzzy, you should consider which SE site is most appropriate for your question. For example, StackOverflow is a good place to post programming questions. – Seamus Dec 28 '18 at 7:41
1

Your script hangs because it does not resume after it run the darkice program.

As you described, when running darkice in the terminal it is starts and runs indefinitely until the user or another process terminates it - in your case the ctrl-c key combination. That's why your terminal window appear frozen.

A simple approach with standard CLI tools to run a program in the background could be the unix terminal program screen, it creates a virtual TTY and comes with raspbian by default.

In your python script you could use the os library to call screen using the os.system() function instead of subprocess.call. Place the following line of instead of your subprocress.call("darkice") in your script:

    os.system("screen -S backgroundtty -D -m darkice")

and your script should resume after the command while screen with darkice will run in the background.

You can stop darkice using the kill command or kill the screen that is running darkice.

    os.system("screen -X -S backgroundtty kill")
  • 1
    Just simply sending darkice to the background with appending & to the command does not work? – Ingo Dec 26 '18 at 17:39
  • @Ingo: that works with a command from the terminal (e.g. bash), but the OP's question concerns starting the process from within a Python program. – Seamus Dec 28 '18 at 7:55
  • 1
    Have you also tried doing this with the subprocess module? I ask because I've read that the subprocess module is currently favored over the os.system calls. I'm just curious... – Seamus Dec 28 '18 at 8:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.