[Subprocess] When you want to execute another Python program in Python code

Introduction

I have multiple codes written in Python, and I want to let one program control the start of them. There are times like that. This time, I will introduce how to execute another program you want to start asynchronously from the program.

environment

Windows10 python 3.7.6 anaconda 20.02

This time's main code

import subprocess

command = [python (File).py (argument)]
proc = subprocess.Popen(command)  #->Command is executed(Do not wait for the end of processing)
result = proc.communicate()  #->Wait for the end

Code example

It's a code without any twist, but I created two files as a trial to see if it works for the time being. call.py is designed to output the given arguments. List the commands by enclosing them in "" separated by spaces.

call.py


import sys
from time import sleep

sleep(1)  #Have them sleep for a second(To check if it is asynchronous)
args = sys.argv[1]
print(args)

main.py


import subprocess

command = ["python","call.py,"I was called!"]
proc = subprocess.Popen(command)
print("Calling")
proc.communicate()

Execution result 0807_subprocesspopen.png

Since the "calling" of main is printed first, You can see that ** not waiting for the process executed by subprocess.Popen () **.

About processing time

When I call it with subprocess.Popen (), I am concerned about ** processing time **. Try calling call_2.py, which performs the following processing for a 1280 x 800 image.

call_2.py


from time import time
import numpy as np
import cv2

start =  time()

img = cv2.imread("sample.png ")
resize = cv2.resize(img,dsize=None,fx=0.5,fy=0.5)  #resize

kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(resize,kernel)  #erode

cv2.imwrite("resize.png ",erosion)

end = time() - start
print(end)

Now when you run it with python main.py and when you run it with python call_2.py I compared the time of the end to be printed. I tried going in and out of the console several times, ** The processing time was about the same whether it was cached or not **. (The actual numbers are omitted because they may depend on the execution environment.)

in conclusion

I showed you how to run another py file with subprocess.Popen (). There was no difference in processing speed in the Windows environment. I'm happy that there is no speed bottleneck. However, it may change if the environment is Linux.

We hope you find this helpful.

Reference material

** Please also refer to the following site ** Official documentation (Python 3.8.5) If the file you want to execute exists in another directory Execute shell command from Python! Summary of how to execute a subprocess with subprocess Execute Linux command from Python with subprocess

** A book that seems to be helpful (I just want to read it ...) ** Expert Python Programming Revised 2nd Edition

Recommended Posts

[Subprocess] When you want to execute another Python program in Python code
[Python] When you want to use all variables in another file
[Python3] Code that can be used when you want to resize images in folder units
I want to write in Python! (1) Code format check
Specifies the function to execute when the python program ends
When you want to plt.save in a for statement
How to execute a command using subprocess in Python
I want to do something in Python when I finish
[Python3] Code that can be used when you want to cut out an image in a specific size
Solution when you want to use cv_bridge with python3 (virtualenv)
[Django] A memorandum when you want to communicate asynchronously [Python3]
I want to be able to run Python in VS Code
When you want to hit a UNIX command on Python
When writing a program in Python
[Small story] A painstaking measure when you have to execute a function before import in Python
[Python] When you want to import and use your own package in the upper directory
If you want to use field names with hyphens when updating firestore data in python
Do you want to wait for general purpose in Python Selenium?
Mode line when you open the appropriate Python code in Vim
If you want to assign csv export to a variable in python
When you run diff in python and want both returncode and output
Articles to read when Blender Python script code doesn't work in 2.80
When you want to replace multiple characters in a string without using regular expressions in python3 series
Python Note: When you want to know the attributes of an object
If you want to count words in Python, it's convenient to use Counter.
I want to do Dunnett's test in Python
To execute a Python enumerate function in JavaScript
I want to create a window in Python
When you want to update the chrome driver.
Personal notes to doc Python code in Sphinx
I want to merge nested dicts in Python
I want to make C ++ code from Python code!
Error when trying to install psycopg2 in Python
I want to display the progress in Python!
Convert cubic mesh code to WKT in Python
How to build an environment when you want to use python2.7 after installing Anaconda3
[Python] I want to know the variables in the function when an error occurs!
I stumbled on the character code when converting CSV to JSON in Python
Use PIL in Python to extract only the data you want from Exif
[Python] What to check when you get a Unicode Decode Error in Django
Python program is slow! I want to speed up! In such a case ...
When you want to erase characters at once in terminal or line unit
[Python3] Code that can be used when you want to change the extension of an image at once
I want to embed a variable in a Python string
I want to easily implement a timeout in python
Things to keep in mind when developing crawlers in Python
Rewrite Python2 code to Python3 (2to3)
I want to write in Python! (2) Let's write a test
Settings when you want to run python-mecab with travis
Even in JavaScript, I want to see Python `range ()`!
A memorandum when writing experimental code ~ Logging in python
Things to keep in mind when copying Python lists
I want to randomly sample a file in Python
What to do if you get an error when importing matplotlib in Python (Mac)
When you want to filter with Django REST framework
I want to work with a robot in python.
Things to note when initializing a list in Python
When you want to play a game via Proxy
I want to replace the variables in the python template file and mass-produce it in another file.
I want to write in Python! (3) Utilize the mock
Use communicate () when receiving output in a Python subprocess