Skip to content

Instantly share code, notes, and snippets.

@jerryvig
Last active February 22, 2020 09:16
Show Gist options
  • Save jerryvig/806651430decd4b6eee9a6894a8c225d to your computer and use it in GitHub Desktop.
Save jerryvig/806651430decd4b6eee9a6894a8c225d to your computer and use it in GitHub Desktop.
Event Objects - Threading Event Objects in Python
import io
import logging
import threading
import requests
logging.basicConfig(level=logging.DEBUG)
def print_response_data(received, data):
"""Waits for the received event and prints the response data."""
logging.info('Starting to wait for the event')
received.wait()
logging.info('data = %s' % data.getvalue())
def main():
"""The main top-level method of the application."""
response_received = threading.Event()
data = io.StringIO()
printer = threading.Thread(target=print_response_data, args=(response_received, data))
printer.start()
logging.info('Making the request')
response = requests.get('http://www.city-data.com/county/Bernalillo_County-NM.html')
data.write(response.text)
response_received.set()
logging.info('We just set the event')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment