Last active
January 1, 2016 16:29
-
-
Save TheWaWaR/8170656 to your computer and use it in GitHub Desktop.
Test Python yield
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 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