0

I have some python script

def task(start, stop):
    #"doing something with start and stop"
    return 

if __name__ == '__main__':
    print ("input start, stop")
    start = int(input())
    stop = int(input())
    task(start,stop)

I run my script by CMD command "python task.py" If I input

start = 1 and stop = 1000

My script running very slow. I could open 10 CMD windows then input

start = 1, stop = 10 in CMD first

start = 11, stop = 20 in CMD second

... and finally start = 90, stop = 100 in final CMD

How can I write a BAT file to run instead of open 10 CMD windows then input by myself.

3
  • 1
    for not just a python file and multi processing ? Commented Feb 12, 2023 at 11:24
  • 1
    It would be much better if your Python script would accept arguments to define start and stop value as options on command line and would prompt the user for the start and/or the stop value only if the appropriate argument was not specified on Python script command line. That would make it possible to use a batch file with a FOR loop much easier. For example, see Command Line Arguments in Python. Commented Feb 12, 2023 at 11:58
  • 1
    It would be also better to run python.exe for interpreting your Python script with suitable start and stop values not more often than the number of cores of your CPU minus 1. One core should be reserved for Windows and all other running processes and the other 1 to X cores can be used to run 1 to X python.exe task.py start=n stop=m or (echo n& echo m) | python.exe task.py if the Python script is not enhanced to support optional arguments for start and stop values. Commented Feb 12, 2023 at 12:04

2 Answers 2

1

You can do it full python

In parallel with multiprocessing

from multiprocessing import Pool

def task(params):
    start, stop = params
    print(start, stop)

with Pool(10) as p:
    results = p.map(task, [(i, i + 9) for i in range(1, 100, 10)])

Sequentially with a loop

def task(start, stop):
    print(start, stop)

for i in range(1, 100, 10):
    task(i, i + 9)

Both will execute the 10 following calls

1 10
11 20
21 30
31 40
41 50
51 60
61 70
71 80
81 90
91 100
Sign up to request clarification or add additional context in comments.

3 Comments

I dont want use multiprocessing because my task is complicated, is there any way to do with batch file?
@duydang I have proposed you a solution with a simple for loop, why not do that ? Also openning 10 terminal IS LIKE multiprocessing, that just means run them at the same time that changes nothing in python in your code, you don't have to change the task code at all
I have try but as I say, my script is complicated. So can not use multiprocessing. In my task function using some global variable. If i using multiprocessing some error come up variable is not defined. So I try using batch file.
0

thanks for @mofi I could write this batch file to solve my problems

@echo off    
setlocal enabledelayedexpansion
for /l %%i in (0,10,99) do (
set /a result=%%i + 9
start cmd /k "(echo %%i & echo !result! ) | python task.py"
)
endlocal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.