Commit c7f40dd2 authored by Toby Hodges's avatar Toby Hodges

a bit more refactoring

parent 1ff4a70a
......@@ -10,41 +10,46 @@ from os import getpid
from time import sleep
from platform import node
def main(argv):
wait_time = 10
list_length = 0
usage = 'hpc_example.py -t <wait time> -l <list length>'
## handle the arguments
try:
opts, args = getopt(argv,"ht:l:",["time=","length="])
except GetoptError as err:
print(err)
print('hpc_example.py -t <wait time> -l <list length>')
exit(2)
for opt, arg in opts:
if opt == '-h':
print('hpc_example.py -t <wait time> -l <list length>')
exit()
elif opt in ("-t", "--time"):
wait_time = max(10, float(arg))
elif opt in ("-l", "--length"):
list_length = max(0, int(arg))
def parseCommand(wait_time=10, list_length=0):
'''
parse the command and set the wait time and list length accordingly
'''
usage = 'hpc_example.py -t <wait time> -l <list length>'
# handle the arguments
try:
opts, args = getopt(argv,"ht:l:",["time=","length="])
except GetoptError as err:
print(err)
print(usage)
exit(2)
for opt, arg in opts:
if opt == '-h':
print(usage)
exit()
elif opt in ("-t", "--time"):
wait_time = max(wait_time, float(arg))
elif opt in ("-l", "--length"):
list_length = max(list_length, int(arg))
return (wait_time, list_length)
def use_memory(list_length):
'''
create a list of length list_length
and return the amount of memory being used by this process in MB
'''
memory_use_list = list(range(list_length))
process = Process(getpid())
memory_used_MB = process.memory_info().rss / 1_000_000
return memory_used_MB
def main(argv):
wait_time, list_length = parseCommand()
print(f'Current host is: {node()}')
print(f'Wait time is: {wait_time} seconds')
print(f'List length is: {list_length}')
## create a list of the required length
memory_use_list = list(range(list_length))
process = Process(getpid())
memory_used_MB = process.memory_info().rss / 1_000_000
## print the maximum memory usage to the screen
print(f'Memory usage: {memory_used_MB} MB')
## wait for the specified number of seconds
sleep(wait_time)
mem_used = use_memory(list_length)
print(f'Memory usage: {mem_used} MB')
sleep(wait_time) # wait for the specified number of seconds
if __name__ == "__main__":
main(argv[1:])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment