In this simple example, we'll introduce the use of gevent within your Python applications. We'll write something that takes in an array of three distinct URLs, and then, we'll spawn an array of gevents in order to retrieve the IP addresses of these URLs using the socket.gethostbyname() function.
We'll then use joinall() for these gevents, giving them a timeout of 2 seconds, and finally, we'll print the array of IP addresses this returns:
import gevent
from gevent import socket
def main():
urls = ['www.google.com', 'www.example.com',
'www.python.org']
jobs = [gevent.spawn(socket.gethostbyname, url)
for url in urls]
gevent.joinall(jobs, timeout=2)
print([job.value for job in jobs])
if __name__ == '__main__':
main()