Skip to content

Instantly share code, notes, and snippets.

@easternnl
Created March 10, 2020 09:01
Show Gist options
  • Save easternnl/657fd35cdd172168f32a47fdbefbd423 to your computer and use it in GitHub Desktop.
Save easternnl/657fd35cdd172168f32a47fdbefbd423 to your computer and use it in GitHub Desktop.
Python threading with subprocess
@echo off
echo This is %0
timeout 3
@echo off
echo This is %0
timeout 3
#!/usr/bin/python
import threading
import time
import subprocess
import logging
def startsubprocess(command):
# start JMeter to load xxx messages on the queue
output = subprocess.run(command, check=True,shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if output.stdout:
logging.info(output.stdout)
if output.stderr:
logging.error(output.stderr)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S")
logging.info("Initialize threads")
thread1 = threading.Thread(target=startsubprocess, args=["thread1.cmd"], daemon=True)
thread2 = threading.Thread(target=startsubprocess, args=["thread2.cmd"], daemon=True)
logging.info("Starting thread1")
thread1.start()
logging.info("Starting thread2")
thread2.start()
logging.info("Main : wait for the threads to finish")
thread1.join()
thread2.join()
logging.info("Main : all done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment