Created
September 19, 2012 16:37
-
-
Save jpcaruana/3750658 to your computer and use it in GitHub Desktop.
non blocking HTTP GET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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