Skip to content

Instantly share code, notes, and snippets.

@gennad
Created May 30, 2012 14:18
Show Gist options
  • Select an option

  • Save gennad/2836618 to your computer and use it in GitHub Desktop.

Select an option

Save gennad/2836618 to your computer and use it in GitHub Desktop.
Python task
import time
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
def timeit(func):
def wrapper(*a, **k):
t1 = time.time()
it = iter(func(*a, **k))
try:
while True:
yield next(it)
except StopIteration:
pass # Or here
print time.time() - t1 # Only successful finish
return wrapper
@timeit
def app(environ, start_response):
start_response("200 OK", [])
for _ in xrange(1000):
yield "12345" * 10 * 1024 * 1024 # 52 428 800 bytes * 1000 about 50 GB
httpd = make_server('', 8080, app)
print "Serving on port 8080..."
httpd.serve_forever()
@gennad
Copy link
Author

gennad commented Jun 1, 2012

Нет, мне наоборот интересно :)
Простое решение... Хм, может что-то вроде этого:

def wrapper(*a, **k):
  t1 = time.time()
  for i in func(*a, **k):
    yield i
  print time.time() - t1  # Only successful finish

return wrapper

Если это не то, скажите, какой правильный ответ )

@temoto
Copy link

temoto commented Jun 1, 2012

Да-да, это именно оно. Просто, надёжно и отвечает всем требованиям. Если есть требование писать время всегда, независимо от ошибок – весь for оборачивается в try/finally. Очень рад.

@gennad
Copy link
Author

gennad commented Jun 1, 2012

=) спасибо

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment