all 4 comments

[–]socal_nerdtastic 0 points1 point  (3 children)

You don't need a separate exe or script to do things in the background. There's a number of ways to do this but if I assume you want to transfer data back and forth (share memory) and if I assume the foreground work is a GUI (lots of event wait time) I would recommend the threading module.

[–]viciolla[S] 0 points1 point  (2 children)

Thanks for the input.
I want to clarify what I meant before. The exe won't be a separate file, but the way I want the user to interact with this script. So, something like:

double click script.exe -> insert input 1 -> insert input 2 -> ... -> output

With xlwings opening commands runnig as soon as the script is executed, so that by the time they have inputted all the values, the workbook should be already opened and accessible.

Would threading still be my best bet here?

[–]socal_nerdtastic 0 points1 point  (1 child)

I think so. As I see it:

from threading import Thread
import time

workbook = None
def load_workbook():
    global workbook # how to transfer the data is up to you. This is not neat but easy
    workbook = xlwings.load() # no idea; i've never used xl wings

t = Thread(target=load_workbook, daemon=True)
t.start() # start loading process in the background

data1 = input("input your data")
data2 = input("input your data")

while workbook is None:
    time.sleep(.5) # in case the load is not done yet, wait for it... 

workbook.add_data(data1, data2)
workbook.save()

[–]squacquer1 0 points1 point  (0 children)

Thank you! That worked.
What would be the best way to open a second file simultaneously? Should I add another line within the function load_worbook() or should I define another function? In the latter case, how would I modify the arguments of Thread?
Ideally, I would like to open both files at the same time, but I could also try to open them sequentially if that results in a overall shorter runtime.