2014-09-18 14:15:57 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Topic: 使用迭代器重写while无限循环
|
|
|
|
|
Desc :
|
|
|
|
|
"""
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
2017-09-19 21:48:46 +08:00
|
|
|
def process_data():
|
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
|
|
|
2014-09-18 14:15:57 +08:00
|
|
|
def reader(s, size):
|
|
|
|
|
while True:
|
|
|
|
|
data = s.recv(size)
|
|
|
|
|
if data == b'':
|
|
|
|
|
break
|
|
|
|
|
# process_data(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reader2(s, size):
|
2016-12-21 15:42:32 +08:00
|
|
|
for data in iter(lambda: s.recv(size), b''):
|
|
|
|
|
process_data(data)
|
2014-09-18 14:15:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def iterate_while():
|
|
|
|
|
CHUNKSIZE = 8192
|
2017-09-19 21:08:59 +08:00
|
|
|
with open('/etc/passwd') as f:
|
|
|
|
|
for chunk in iter(lambda: f.read(10), ''):
|
|
|
|
|
n = sys.stdout.write(chunk)
|
2014-09-18 14:15:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
iterate_while()
|