Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port introduction snippets to use async def and t.i.t.react #20

Open
graingert opened this issue Aug 23, 2022 · 8 comments
Open

port introduction snippets to use async def and t.i.t.react #20

graingert opened this issue Aug 23, 2022 · 8 comments

Comments

@graingert
Copy link
Member

No description provided.

@graingert
Copy link
Member Author

looks like this is blocked by #10 or #2 ?

@adiroiban
Copy link
Member

I don't know what is the scope of this issue, but it shouldn't be blocked.

The from page is just a simple HTML page with the content here https://github.com/twisted/twisted.github.io/blob/main/index.html

@graingert
Copy link
Member Author

I'm worried if I add more HTML it will make it harder to switch to rst

@graingert
Copy link
Member Author

graingert commented Aug 23, 2022

I don't know what is the scope of this issue

so this code:

from twisted.internet import protocol, reactor, endpoints

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory())
reactor.run()

becomes:

import math

from twisted.internet import protocol, endpoints, task

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

async def echo_server(reactor):
    await endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory())
    await task.deferLater(reactor, math.inf, lambda: pass)
    return 0

def main():
    return task.react(echo_server)

if __name__ == "__main__":
    sys.exit(main())

@adiroiban
Copy link
Member

adiroiban commented Aug 23, 2022

OK. I have created a PR. we can discuss the new changes there.

I have no idea how a good example should look like...

Should it contain comments... or the code should be simple enough so that the comments are not needed.

Should it contain "hacks" .

To me, the line below looks like a hack :)

await task.deferLater(reactor, math.inf, lambda: pass)

The result is here https://deploy-preview-21--twisted-pr-preview.netlify.app/

@graingert
Copy link
Member Author

Would be nice if you could just do

async with server(port=0) as server:
    print (f"listening on port {server.port}, waiting for ctrl+c")
    await sleep_forever()

Eh?

@adiroiban
Copy link
Member

:)

This is very nice and I think a big selling point for new users.

In real life, not having a context manager is not a big deal for me.

I have created the PR, just to move things forward.

Feel free to suggest whatever you think is best and we can request feedback from others.

Thanks

@JudgeGregg
Copy link
Contributor

I'm worried if I add more HTML it will make it harder to switch to rst

Feel free to add as much HTML as you want, I don't think it would impact the switch to RST at all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants