Skip to content

Instantly share code, notes, and snippets.

@mikolasan
Last active July 19, 2022 18:58
Show Gist options
  • Save mikolasan/21a526373a1d35216f933dbb8b2f3d43 to your computer and use it in GitHub Desktop.
Save mikolasan/21a526373a1d35216f933dbb8b2f3d43 to your computer and use it in GitHub Desktop.
Python zhdun
from threading import Thread
from time import sleep
# globals
result = None
def result_will_be_soon():
sleep(1)
global result
result = 42
print('-- result is known now')
def ask_for_result():
worker = Thread(target=result_will_be_soon)
worker.start()
def happy_about_result():
assert result != None, 'no result: sad :('
print(f'The answer is {result}!')
def main():
ask_for_result() # for example, send request to the server
# wait for results
happy_about_result() # continue program execution
if __name__ == '__main__':
main()
import asyncio
from threading import Thread
from time import sleep
# globals
result = None
def result_will_be_soon():
sleep(1)
global result
result = 42
print('-- result is known now')
def ask_for_result():
worker = Thread(target=result_will_be_soon)
worker.start()
async def check_for_results():
while True:
await asyncio.sleep(1)
if result != None:
break
def happy_about_result():
assert result != None, 'no result: sad :('
print(f'The answer is {result}!')
async def main():
ask_for_result() # for example, send request to the server
await check_for_results() # wait for results
happy_about_result() # continue program execution
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment