Skip to content

Instantly share code, notes, and snippets.

@welvet
Created March 17, 2015 22:20
Show Gist options
  • Save welvet/b2cbf2567c7d48345021 to your computer and use it in GitHub Desktop.
Save welvet/b2cbf2567c7d48345021 to your computer and use it in GitHub Desktop.
Python pool
import urllib2
import json
import StringIO
import time
import gzip
from threading import Thread, Lock
url = """http://ya.ru"""
cnt = [0, 0]
l = Lock()
def load(t):
for i in range(1, 10000):
request = urllib2.Request(url, headers={
"Accept-Encoding": "gzip, deflate",
"Cookie": "a=b",
})
response = urllib2.urlopen(request)
buf = StringIO.StringIO(response.read())
data = gzip.GzipFile(fileobj=buf).read()
try:
json.load(StringIO.StringIO(data))
with l:
cnt[0] += 1
except:
# print "fail: %s" % data
with l:
cnt[1] += 1
for i in range(0, 20):
t = Thread(target=load, args=[i])
t.start()
while True:
print cnt
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment