Ticket #3834 enhancement closed fixed

Opened 4 years ago

Last modified 2 years ago

TCP client howto missing reactor.run() in one code sample

Reported by: itamar Owned by: thijs
Priority: normal Milestone:
Component: core Keywords: documentation, easy
Cc: thijs, mwh, exarkun Branch: branches/examples-reactor-run-3834-2
(diff, github, buildbot, log)
Author: thijs Launchpad Bug:

Description

In the "Simple, single-use clients" section there's a full example, but it's missing a reactor.run().

Change History

1

  Changed 4 years ago by exarkun

  • keywords easy added

2

follow-up: ↓ 5   Changed 4 years ago by exarkun

 http://twistedmatrix.com/projects/core/documentation/howto/rdbms.html#auto5 is missing it too. I bet lots of other docs are missing it, too.

3

  Changed 4 years ago by glyph

  • owner changed from glyph to thijs

4

  Changed 4 years ago by thijs

  • branch set to branches/examples-reactor-run-3834
  • branch_author set to thijs

(In [27112]) Branching to 'examples-reactor-run-3834'

5

in reply to: ↑ 2   Changed 4 years ago by thijs

  • cc thijs added
  • keywords documentation, easy, review added; documentation easy removed
  • owner thijs deleted

Replying to exarkun:

 http://twistedmatrix.com/projects/core/documentation/howto/rdbms.html#auto5 is missing it too. I bet lots of other docs are missing it, too.

Seems this is missing more than just as a reactor.run() call but also the imports, which is true for all of the examples on that page. If you can fix these imports, please do. Putting up the changes for the original ticket description for review.

6

follow-up: ↓ 8   Changed 4 years ago by mwh

  • cc mwh added
  • owner set to thijs
  • keywords easy added; easy, review removed

I'm not sure adding reactor.run() to the example in clients.xhtml without adding a reactor.stop() as well. It's also a slightly odd example. Do you think changing it to this:

def gotProtocol(p):
    p.sendMessage("Hello")
    defer.maybeDeferred(p.transport.loseConnection).addCallback(reactor.stop)

would make more sense?

All the other changes look good.

7

  Changed 4 years ago by mwh

Oops, I forgot something: thanks for working on this!

8

in reply to: ↑ 6   Changed 4 years ago by thijs

  • keywords easy, review added; easy removed
  • owner thijs deleted

Replying to mwh:

I'm not sure adding reactor.run() to the example in clients.xhtml without adding a reactor.stop() as well. It's also a slightly odd example. Do you think changing it to this: def gotProtocol(p): p.sendMessage("Hello") defer.maybeDeferred(p.transport.loseConnection).addCallback(reactor.stop) would make more sense?

Well, if I do that and run the example it throws this error:

Unhandled error in Deferred:
Traceback (most recent call last):
Failure: twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 61: Connection refused.

Any idea?

9

  Changed 4 years ago by mwh

Well, the existing code does that if nothing is listening.

I'm having a hard time getting this correct, so I don't know if I'm a good person to review this branch :) I've got as far as this:

from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientCreator
from twisted.python import log

class Greeter(Protocol):
    def sendMessage(self, msg):
        self.transport.write("MESSAGE %s\n" % msg)

def gotProtocol(p):
    p.sendMessage("Hello")
    return p.transport.loseConnection()

c = ClientCreator(reactor, Greeter)
d = c.connectTCP("localhost", 1234)
d.addCallback(gotProtocol).addErrback(log.err).addCallback(
    lambda ignored: reactor.stop())
reactor.run()

But the reactor.stop() gets called before the data is sent.

10

  Changed 4 years ago by mwh

  • owner set to thijs
  • keywords easy added; easy, review removed

I should have moved this out of the review state.

11

  Changed 4 years ago by thijs

  • owner changed from thijs to exarkun

assigning to exarkun for help/advice with this ticket.

12

follow-up: ↓ 15   Changed 4 years ago by exarkun

  • cc exarkun added
  • owner changed from exarkun to thijs

Since the code is already doing arbitrary stuff with delayed calls, I think you just want to call reactor.stop after some arbitrary delay that's long enough to let the message be sent most of the time.

That, or make it actually work right, and put the reactor.stop call in the protocol's connection lost (which defeats the purpose of the example, which seems to be about using protocol instances from code that's not part of the protocol class).

So yea, reactor.callLater(3, reactor.stop).

13

  Changed 3 years ago by thijs

  • status changed from new to assigned

14

  Changed 2 years ago by thijs

(In [30553]) correct stop call, add news file. refs #3834

15

in reply to: ↑ 12   Changed 2 years ago by thijs

  • keywords easy, review added; easy removed
  • status changed from assigned to new
  • owner thijs deleted

Replying to exarkun:

So yea, reactor.callLater(3, reactor.stop).

Merged forward and replaced that call.

16

  Changed 2 years ago by exarkun

  • owner set to thijs
  • keywords easy added; easy, review removed

There's a conflict in clients.xhtml now. Can you merge forward and resolve it? Also, the news file doesn't reflect the style described on ReviewProcess#Newsfiles. Can you re-arrange it so it does, and mention the udp and client howtos specifically?

Thanks.

17

  Changed 2 years ago by thijs

  • status changed from new to assigned

Thanks for the review, i'll look into it.

18

  Changed 2 years ago by thijs

  • branch changed from branches/examples-reactor-run-3834 to branches/examples-reactor-run-3834-2

(In [30594]) Branching to 'examples-reactor-run-3834-2'

19

  Changed 2 years ago by thijs

  • keywords easy, review added; easy removed
  • status changed from assigned to new
  • owner thijs deleted

Applied the changes in a new branch. Doc builder is  happy.

20

  Changed 2 years ago by jerub

  • owner set to jerub
  • status changed from new to assigned

21

  Changed 2 years ago by jerub

  • owner changed from jerub to thijs
  • keywords easy added; easy, review removed
  • status changed from assigned to new

I've applied this branch to my working copy, read through the examples in the two affected files and while 2 of the examples are runnable, the third simply has an ellipsis and is not runnable - I cut+paste what was required from the other example to splice together something that would work, and it did.

What I'm saying is, I'm glad these examples have been improved, I've tested them, and they work, please merge.

22

  Changed 2 years ago by <automation>

  • owner thijs deleted

23

  Changed 2 years ago by thijs

  • status changed from new to assigned
  • owner set to thijs

Thanks for the review.

24

  Changed 2 years ago by thijs

  • status changed from assigned to closed
  • resolution set to fixed

(In [30759]) Added missing reactor.run() calls in the UDP and client howto documents.

Author: thijs Reviewer: mwh, exarkun, jerub Fixes: #3834

Note: See TracTickets for help on using tickets.