Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
2

How to close file handles when running multiple python EMS simulations?

asked 2023-04-02 09:12:05 -0600

SebsCubs's avatar

updated 2023-04-03 09:39:29 -0600

Hi, I have a python script where I am trying to run thousands of simulations in a sequential fashion. I am making a copy of the whole energyplus directory and instantiating an API object each time. while I keep a reinforcement learning agent in a separate object interacting with the energyplus wrapper object for each run.

After certain number of runs I am always hitting the error of "Too many open files" or Energy plus itself not being able to access a file because it has hit the OS limit for file handles in a single process.

If I run the command lsof | awk '{ print $1 " " $2; }' | sort -rn | uniq -c | sort -rn | head -15 in linux I find that my python3 process keeps opening handles as the execution time passes. image description

If I try listing the open file handles I can see it's a bunch of out files from energyplus which I am manually deleting but the handle remains attached to the python process, even with the file deleted. image description

I saw one possible solution is to increase the maximum number of handles available for a single process in Linux, however this is not a scalable solution since I am planning to run even millions of simulations in a single run.

Is there a way of closing the file handles opened by the energyplus simulation? Any idea of a different approach for this would also be widely appreciated.

Thanks.

edit retag flag offensive close merge delete

Comments

How do you launch the E+ simulation? Can you share a sample code? If you use python multiprocessing/threading, did you you forget to join() the threads/processes?

Julien Marrec's avatar Julien Marrec  ( 2023-04-03 02:07:39 -0600 )edit

@Edwin has two examples: 02_threaded.py and 03_multiprocessed.py in Myoldmopar/EnergyPlusAPIHelper that you may want to check out

Julien Marrec's avatar Julien Marrec  ( 2023-04-03 02:13:18 -0600 )edit
1

Hi @Julien. Thank you for your answer. I was basically executing the function EnergyPlusAPI.runtime.run_energyplus() sequentially in a for loop. This caused the python process to accumulate the handles of each energyplus run.

I took a look into the multiprocessing API of python and implemented each run as a process which solved the issue. Thanks!

SebsCubs's avatar SebsCubs  ( 2023-04-03 08:49:32 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-04-03 08:53:36 -0600

SebsCubs's avatar

Thank you for your answers. I was basically executing the function EnergyPlusAPI.runtime.run_energyplus() sequentially in a for loop. This caused the python process to accumulate the handles of each energyplus run.

I took a look into the multiprocessing API of python and implemented each run as a process which solved the issue. Thanks!

A pseudocode for what I did could be like this:

queue = Queue() #Global queue object for the threads(Process)

for ep in range(a2c_agent.EPISODES):
    #--- Process handler ###
    proc = Process(target=Energyplus_manager,args=(queue,)) 
    proc.start()
    while(proc.exitcode == None): #As long as the thread is not closed
        a2c_agent.remember(queue) #Retrieve info from the thread for the RL agent, timeout if no more data is being loaded
    proc.join()

    average = a2c_agent.evaluate_model(episode_rewards[-1], (ep+1)) # evaluate the model
    a2c_agent.replay() # train the network
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

Asked: 2023-04-02 09:12:05 -0600

Seen: 157 times

Last updated: Apr 03 '23