>>> classData(object): ... def__init__(self, *args): ... self._data = list(args) ... def__iter__(self): ... for x in self._data: ... yield x ... >>> d = Data(1, 2, 3) >>> for x in d: ... print(x)
>>> (i for i in [1,2,3]) <generator object <genexpr> at 0x10fd7ed38>
>>> [i for i in [1,2,3]] [1, 2, 3]
传值
举个例子
1 2 3 4 5 6 7 8 9
deffoo(n): for i in range(n): re = yield i print"v_%s = %s" % (i, re)
defcoroutine(): print"coroutine start..." result = lambda x: print('lala %s' % x) whileTrue: s = yield result result = 'result: {}'.format(s)
# >>> c = coroutine() # >>> c # <generator object coroutine at 0x10fb9eab0> # >>> xx = next(c) # conr start.. # >>> xx # <function coroutine.<locals>.<lambda> at 0x10fe6d268>
for callback in callbacks: self._run_callback(callback)
def _run_callback(self, callback): """Runs a callback with error handling.
For use in subclasses. """ try: ret = callback() if ret is not None and is_future(ret): # 将依据 self.add_future(ret, lambda f: f.result()) except Exception: self.handle_callback_exception(callback)
def__init__(self, gen, result_future, first_yielded): if self.handle_yield(first_yielded): self.run()
defhandle_yield(self, yielded): # ... ifnot self.future.done() or self.future is moment: self.io_loop.add_future( self.future, lambda f: self.run()) returnFalse
# ioloop
defadd_future(self, future, callback): """Schedules a callback on the ``IOLoop`` when the given `.Future` is finished.
The callback is invoked with one argument, the `.Future`. """ assert is_future(future) callback = stack_context.wrap(callback) future.add_done_callback( lambda future: self.add_callback(callback, future))
defstart(self): whileTrue: try: event_pairs = self._impl.poll(poll_timeout) except Exception as e: # Depending on python version and IOLoop implementation, # different exception types may be thrown and there are # two ways EINTR might be signaled: # * e.errno == errno.EINTR # * e.args is like (errno.EINTR, 'Interrupted system call') if errno_from_exception(e) == errno.EINTR: continue else: raise
# Pop one fd at a time from the set of pending fds and run # its handler. Since that handler may perform actions on # other file descriptors, there may be reentrant calls to # this IOLoop that update self._events self._events.update(event_pairs) while self._events: fd, events = self._events.popitem() try: fd_obj, handler_func = self._handlers[fd] handler_func(fd_obj, events) except (OSError, IOError) as e: if errno_from_exception(e) == errno.EPIPE: # Happens when the client closes the connection pass else: self.handle_callback_exception(self._handlers.get(fd)) except Exception: self.handle_callback_exception(self._handlers.get(fd)) fd_obj = handler_func = None
# 1. <generator object getdata_with_sleep at 0x10f713900> # 2. <bound method TestCls.test_block of <__main__.TestCls object at 0x10f711f28>> # 3. <Future at 0x10f711f60 state=running> # 1. i am not blocked # inner send result called !!!! # 4... finish 333
## AF_INET is the address family that is used for the socket you're creating (in this case an Internet Protocol address). The Linux kernel, for example, supports 29 other address families such as UNIX sockets and IPX, and also communications with IRDA and Bluetooth (AF_IRDA and AF_BLUETOOTH, but it is doubtful you'll use these at such a low level).
# For the most part sticking with AF_INET for socket programming over a network is the safest option.
## Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A, B, C", they will arrive in the same order - "A, B, C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries. # http://www.tutorialspoint.com/unix_sockets/what_is_socket.htm
# Giving 0 as protocol to socket just means that you want to use the default protocol for the family/socktype pair. In this case that is TCP, and thus you get the same result as with IPPROTO_TCP. orz
sock.listen(backlog) # {int} 128
# The maximum length of the queue of pending connections
通过 netutil.py 注册到ioloop
1 2 3 4 5
for sock in sockets: self._sockets[sock.fileno()] = sock add_accept_handler(sock, self._handle_connection, io_loop=self.io_loop)