Python Forum
run 2 commands at the same time ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run 2 commands at the same time ?
#1
Hello ,
I have a small project that monitor voltage using ina3221
when I run it in main infite loop it's work as it should - no problem there.

I want to be abele to run my "Check Voltage Function" while I send a commnd to device , so I will be able to check the used voltage

this is what I have :

import sleep
import date 
import SDL_Pi_INA3221

import os
import sys
import random

TestSound = ["/Music/latvia.mp3","/Music/russia.mp3","/Music/France.mp3"]

CH3 = 3

def Check_Voltage():
     Voltage = ina3221.getBusVoltage_V(CH3)
     Current = ina3221.getCurrent_mA(CH3)
     print("Volume AMP Voltage : " +  str(Voltage))
     print("Volume AMP Current : " +  str (Current))

 while 1:
     i = (i + 1)
     date = datetime.now().strftime('%d-%m-%y %H-%M-%S')
     print(i, date)
     select = input('Press 1 to send random MP3 \n\r')
     if select == '1':
         os.system("mpg123 -o alsa:hw:2,0 " + random.choice(TestSound))
         Check_Voltage()
     else:
        print ('no sound will play , wait for next time! ')
        sleep(5)
I want after I press "1" to run the function it will also send me the "play command "
so I will know how much voltage\current the device is using to play to file


********
I found this
[inline=https://stackoverflow.com/questions/7168508/background-function-in-python][/inline]
and try to add this :
thread_function = threading.Thread(target=Check_Voltage)
        thread_function.start()
        os.system("mpg123 -o alsa:hw:2,0 " + random.choice(TestSound))
but it only run me the "Check_Voltage" 1 time before running the "play" command
Thanks ,
Reply
#2
You don't need a thread because the process is started as a child process of Python.

It's only blocking if you use from subprocess the functions call, check_call, check_output, run (and those I've forgotten).

The os.system should be deprecated since Python 3.0. Don't use it.

Here an example which was tested on Windows. It should work also on Linux if you replace the play function.
import time
from pathlib import Path
from subprocess import Popen


def play(file):
    """
    I was testing on windows. No mpg123 available...
    and additional I had problems to use regular paths
    """
    my_vlc_player = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
    file_uri = Path(file).as_uri()
    return Popen([my_vlc_player, "--play-and-exit", file_uri])


def measure(file):
    proc = play(file)
    while proc.returncode is None:
        time.sleep(0.1)
        # how fast?
        
        # your code for measurement
        print("Voltage/Current")
        
        # proc.poll() sets returncode after program has
        # been finished
        proc.poll()
    print("Playing sound finished")


# my testing
measure("C:/Windows/Media/Alarm02.wav")
You can remove the Path import afterwards. I used it only to convert the file path to a file-uri.
I don't know why, but the vlc I've installed seems to like only file-uri.
But mpeg123 should not have this strange behavior.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
You can start the process with the subprocess.Popen() constructor. No thread is needed as you are running an external process.
import subprocess as sp
 
while True:
    i = (i + 1)
    date = datetime.now().strftime('%d-%m-%y %H-%M-%S')
    print(i, date)
    select = input('Press 1 to send random MP3 \n\r')
    if select == '1':
        with sp.Popen([
            "mpg123", "-o", "alsa:hw:2,0",
            random.choice(TestSound)]) as proc:
            sleep(0.2) # small delay perhaps 
            Check_Voltage()
    else:
        print ('no sound will play , wait for next time! ')
        sleep(5)
Reply
#4
yes it's working

Thank you for the example and explain!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020