Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Last active August 29, 2015 14:27
Show Gist options
  • Save squeaky-pl/c657e1dd2f68a5d0a0e3 to your computer and use it in GitHub Desktop.
Save squeaky-pl/c657e1dd2f68a5d0a0e3 to your computer and use it in GitHub Desktop.
Terminate me by kill -TERM 90
http://example.org trying to contact
http://softwearautomation.com trying to contact
http://example.org <!doctype html>
http://softwearautomation.com <!DOCTYPE html>
http://example.org trying to contact
http://example.org <!doctype html>
http://softwearautomation.com trying to contact
http://softwearautomation.com <!DOCTYPE html>
http://example.org trying to contact
http://example.org <!doctype html>
http://softwearautomation.com trying to contact
http://softwearautomation.com <!DOCTYPE html>
Exiting
import gevent
import gevent.monkey
from contextlib import closing
gevent.monkey.patch_all()
import urllib
import signal
import os
INTERVAL = 10
urls = ['http://example.org', 'http://softwearautomation.com']
def sigterm_handler(workers):
for w in workers:
w.kill()
def worker(url):
while 1:
print url, 'trying to contact'
with closing(urllib.urlopen(url)) as fp:
line = fp.readline()
print url, line
gevent.sleep(INTERVAL)
def main():
workers = [gevent.spawn(worker, u) for u in urls]
gevent.signal(signal.SIGTERM, sigterm_handler, workers)
import os
print "Terminate me by kill -TERM", os.getpid()
gevent.joinall(workers)
print "Exiting"
if __name__ == '__main__':
main()
import gevent
import gevent.monkey
from contextlib import closing
gevent.monkey.patch_all()
import urllib
INTERVAL = 10
urls = ['http://example.org', 'http://softwearautomation.com']
def worker(url):
while 1:
print url, 'trying to contact'
with closing(urllib.urlopen(url)) as fp:
line = fp.readline()
print url, line
gevent.sleep(INTERVAL)
def main():
workers = [gevent.spawn(worker, u) for u in urls]
gevent.joinall(workers)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment