0

I try to design a programm in VBA (used within a simulation software) which is able to call Python for some calculations and receive the result to proceed. The VBA-Python call will happen many times.

My first idea is based on a text file communication, e.g. something like this:

in VBA:
do something

write text file 'calc_todo.txt' in specific directory

while not exists 'calc_finished.txt':
    wait 1 second

read 'calc_finished.txt'

delete'calc_finished.txt'

delete'calc_todo.txt'

do something

write text file 'calc_todo.txt' in specific directory

... repeat



in Python:
do something

while not exists 'calc_todo.txt':
    wait 1 second

read 'calc_todo.txt'

do calculations based on 'calc_todo.txt'

write 'calc_finished.txt'

delete'calc_todo.txt'

do something

while not exists 'calc_todo.txt':
    wait 1 second

... repeat

I have done something similar in past and unfortunately there are a lot of things I do not like:

  • fixed waiting time of e.g. 1 second might slow down performance
  • if something breaks VBA and/or Python will get stuck in a while loop or run in an error
  • to fix the second issue, error handling with initialisation can be implemented but last time it was a mess

What would be a more professional way on how to handle such communication?

3
  • A file can exist (by name) before all its contents have been written and the file is available and populated as expected. It's size however can be a guide. Commented Feb 16, 2021 at 9:53
  • 1
    If you want to ensure that a file is ready for processing, create the first first with a temporary name and rename it only after finish writing. Commented Feb 16, 2021 at 10:16
  • 1
    I have used this approach successfully, creating a shell and running a script stackoverflow.com/questions/39356710/… Commented Feb 16, 2021 at 10:32

0

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.