Skip to content

Instantly share code, notes, and snippets.

@TheWaWaR
Last active January 1, 2016 16:29
Show Gist options
  • Save TheWaWaR/8170656 to your computer and use it in GitHub Desktop.
Save TheWaWaR/8170656 to your computer and use it in GitHub Desktop.
Test Python yield
#coding: utf-8
from datetime import datetime
import time
# ==============================================================================
# Test yield
# ==============================================================================
def test_yield():
for i in range(4):
print '[%s] >> Start processing......%d' % (datetime.now(), i)
time.sleep(2)
print '[%s] >> DONE ......%d' % (datetime.now(), i)
yield i
for t in test_yield():
print '[%s] >> %d' % (datetime.now(), t)
print '----------------------------------------'
print '============================================================'
# ==============================================================================
# Test generator
# ==============================================================================
def process(i):
print '[%s] >> Start processing......%d' % (datetime.now(), i)
time.sleep(2)
print '[%s] >> DONE ......%d' % (datetime.now(), i)
return i
for t in (process(i) for i in range(4)):
print '[%s] >> %d' % (datetime.now(), t)
print '----------------------------------------'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment