Skip to content

Instantly share code, notes, and snippets.

@jpcaruana
Created September 19, 2012 16:37
Show Gist options
  • Save jpcaruana/3750658 to your computer and use it in GitHub Desktop.
Save jpcaruana/3750658 to your computer and use it in GitHub Desktop.
non blocking HTTP GET
# -*- coding: utf-8 -*-
from datetime import datetime
import multiprocessing, time, urllib2
class Browser(object):
def __init__(self):
self.p = None
def request(self, s):
if self.p and self.p.is_alive():
print 'Kill ' + str(self.p.pid)
self.p.terminate()
self.p = multiprocessing.Process(target=self.fetch, args=(s,))
self.p.start()
def fetch(self, s):
print '--------------------'
print s
print datetime.now()
urllib2.urlopen('http://desktop/neko').read() # prend 5s...
print 'Response OK ' + s
print datetime.now()
b = Browser()
b.request('first')
time.sleep(3)
b.request('second')
time.sleep(10)
b.request('third')
print 'Terminé'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment