I am looking for a way to start multiple process efficiently with low priority in Windows. I tried :
def run(command):
# command['Program.exe args1 args2','output_file']
try :
p = subprocess.Popen(command[0] , stdout = command[1])
psutil.Process(p.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
p.wait()
except Exception as e:
print(e)
raise SystemExit
The problem is : the low priority is not set immediately. I get some freeze at the beginning. When I look closer in the process window I can see the priority of the application starting at high_priority and switch to low_priority.
I would like to start immediately at low priority or find another way to block the CPU usage (100% right now).
Then I use the run command inside a multiprocessing pool (few second for each run).
def safe_run(args):
"""Call run(), catch exceptions."""
try:
run(args)
except Exception as e:
print(args[0])
print(e)
def parallel(commands,nb_proc):
# populate files
# start processes
if len(commands) < 10:
nb_proc = 1
print('Use of {} cpus\n'.format(nb_proc))
pool = mp.Pool(nb_proc)
pool.map(safe_run, commands, chunksize=1)
UPDATE
Test.exe is a fortran code :
integer function NumArguments()
integer :: IARGC
NumArguments = IARGC()
end function
subroutine GetArgument(Idx,Argument)
integer, intent(in) :: Idx
character(LEN=*), intent(out) :: Argument
call GETARG(Idx,Argument)
end subroutine
program Console
implicit none
integer, parameter :: INTEG = SELECTED_INT_KIND(9)
integer(INTEG), parameter :: MAX_STRING_LEN = 1024_INTEG
character(LEN=MAX_STRING_LEN) :: FileName
integer(INTEG) :: i
call GetArgument(1,FileName)
! Body of Console
!print *, 'Hello World'
!print *, FileName
call sleep(5)
open(unit=1, file=FileName,status='new')
Do i=1,1000,1
write(1,*) i
Enddo
close(unit=1)
end program Console
Full code :
# -*- coding: utf-8 -*-
"""
"""
###############################################################################
###############################################################################
#
# IMPORT & INIT
#
###############################################################################
###############################################################################
import psutil
import subprocess
import time
import multiprocessing.dummy as mp
import os
TEST_EXE = "Console.exe"
nb_proc = 4
###############################################################################
###############################################################################
#
# FUNCTION
#
###############################################################################
###############################################################################
def run(command):
try :
print(command[0])
psutil.Process().nice(psutil.BELOW_NORMAL_PRIORITY_CLASS) # lower priority
p = subprocess.Popen(command[0] , stdout = command[1])
psutil.Process().nice(psutil.BELOW_NORMAL_PRIORITY_CLASS) # lower priority
p.wait()
except:
print('Point {} fail'.format(point))
raise SystemExit
def safe_run(args):
"""Call run(), catch exceptions."""
try:
run(args)
except Exception as e:
print('{} error'.format(args[0]))
def parallel(commands,nb_proc):
print('Use of {} cpus\n'.format(nb_proc))
pool = mp.Pool(nb_proc)
pool.map(safe_run, commands, chunksize=1)
###############################################################################
###############################################################################
#
# MAIN SCRIPT
#
###############################################################################
###############################################################################
current_dir = os.path.abspath('')
print('\nCurrent directory {}'.format(current_dir))
t1 = time.time()
logfiles = list()
commands = list()
logfiles_obj = list()
for step in range(100):
logfile = open(os.path.join(current_dir,'logfile_'+ str(step) + '.out'), 'w')
args = TEST_EXE + ' ' + os.path.join(current_dir,'output_'+str(step) + '.txt')
temp = (args,logfile)
commands.append(temp)
# run in parallel
print("Calculation running ...\n")
parallel(commands,nb_proc)
for log in logfiles_obj:
log.close()
# time for running all the point and complete
t2 = time.time()
print ("\n ########## Overall time : %5.2f secondes ##########" %(t2 - t1))
print("\n ########## Correct ending ##########")