import, function, concurrency
Your code is made of many files. At least, I hope it is. Your favorite language will have keywords like `import` or `require` or `include`. Python isn't my favorite language, but it's fine... it uses `import`.
Your files get stupid really fast if you can't encapsulate the instructions. Functions are a basic way to do that.
Your program gets annoying really fast if you have to wait for things to finish before more things happen. Sometimes that's what you want, but sometimes it isn't. When it isn't, you're looking for concurrency.
Here are python files that do all that stuff.
import time
import threading
def long_running_process(sleep_time):
time.sleep(sleep_time)
print('thred done')
def lets_go(t=10):
thread = threading.Thread(target=long_running_process, args=(t,))
thread.start()
print('text to print after thread starts')
lets_go()
# main.py
import threadsy
def do_things(whichthings='none'):
if whichthings == 'all':
print("Hmm, I wonder when this will print.")
threadsy.lets_go(7)
else:
print("Meh.")
print("When would you expect this to print?")
if __name__ == '__main__':
do_things('all')
% python threadsy.py
'text to print after thread starts'
'thred done'
% python main.py
'text to print after thread starts'
'When would you expect this to print?'
'Hmm, I wonder when this will print.'
'text to print after thread starts'
'thred done'
'thred done'
Why is the thread `long_running_process` running twice when we execute main.py? What can we do?