This is a very similar question to a discussion thread that I am part of for Lua in Codea (Lua development environment for the iPad): See here. On that page, follow se24vad’s use of a thread queue. Note that code on that page is not necessarily portable, since Codea provides some functionality not built in to Lua. I point to this page for the ideas, not for the specific code.
Basically, the idea is that you need to queue up the functions in a thread queue (i.e. Lua coroutines). In his particular implementation you do not move on to a new coroutine until the one at the top (index 1 in the queue table) is complete. In other words, the coroutine calls are blocking.
You will want to implement a coroutine queue, with each of your os.execute
calls as a separate coroutine.
Now, how that fits in to the rest of your code, I am not sure. I do not know what code you have that controls when your page refresh happens. You would need some sort of controller code to keep track of the coroutine queue.
If a thread queue isn’t possible in your use-case, then maybe the python scripts need to somehow report completion back to the browser. When a new execute call is made, it checks the completion variable first before proceeding.