python - twisted callback function confusion -


I am working on a twisted tutorial for learning more dragon and it seems that I went to a road block here I am Below is the "callback" of a reactor of the given thread () function. What I do not understand is how the part works except how it works.

The way I read the code is that if bytes = + self.sock.recv (1024) causes a block then it reaches the following part of the code

  if e.args [0] == errno.EWOULDBLOCK: break  

Then it would have continued for the following:

  is not a byte: print 'Task% d Ends'% self.task_num return main.CONNECTION_DONE other: msg =' Task% d:% d by% s by% s print message% s (myself. The hard part is that if it has been blocked There is nothing in the byte variable and "printed" happens "but it is not, or at least this" "something like" got 0 bytes, but it is not. The code has to face a block from the block call, it leaves the above part completely, can someone explain why this is happening?  

The output is something like this:

  Work 1: 127.0.0.1:10000 to 30 bytes of poetry work 3: 10 127.0.0. From 1: 120002, the bytes of the poem Task 1: 127.0.0.1: 0000 received 30 bytes of the poem, by Task 3: 127.0.0.1120002, 10 bytes of the poem has been received by Task 1: 127.0.0.1 by 30 bytes of the poem:  

This is a complete work:

From Task 1: 127.0.0.1:10000 received 3 bytes of poetry from 10000 Work 3: 127.0.0.1120002.
  def doRead (self): bytes = '' right: try: bytes + = self.sock.recv (1024) if not bytes: break except the socket. Terror, e: # I do not understand this part if E.A.G.G [0] == is not wrong. Eldblock: Brake returns main. CONNECTION_LOST if not bytes: print 'Task% d End'% self.task_num return main.CONNECTION_DONE other: msg = 'Job% d: found% D bytes o% S' Print msg% (self.task_num, len (bytes), The entire poem from self.format_addr ()) self.poem + = bytes  

has been pasted here:

The issue is that the method doRead is only when the socket is" ready to read ": either it has some data or No, all this has been done (and then, read 0 ), so the solution to your problem The doRead function can not be in - this code has calling , when only appropriate.

This code is in the twisted "reactor", whose poetic sketch example __init __ (via the addReader method of reactor) From). If you want to understand the mechanisms of twisted in real baseless depth, then you seem to be in the right place (in fact, the net in studying twisted sources);


Comments