question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a class accept a socket?

See original GitHub issue

I want to use asyncssh as server in a child process. Parent process use socketserver.ForkingTCPServer.

Like this:

server = socketserver.ForkingTCPServer((host, port), SSHHandler)
server.serve_forever()

class SSHHandler(socketserver.StreamRequestHandler)

    def handle(self):
        # accept a ssh socket, make a ssh connection and do something
        async_ssh_server(socket=self.request)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ronfcommented, Aug 2, 2022

In order to take advantage of multiple processes, you’d need for each process to create its own aysncio event loop after it is forked, and to get maximum benefit from that you’d really want to create a pool of such processes of roughly the size of the number of CPU cores on the system. With the forking socket server, you’ll end up with a separate process for each connection, which can lead to performance problems if the number of connections is much larger than the number of available cores.

I must admit that I haven’t spent a lot of time trying to get Python asyncio working with a pool of processes. If you truly don’t need to share any state across connections, it’s actually probably easier to just run multiple instances of your server. You can allow multiple instances to listen on the same port if you add reuse_port=True to the arguments when setting up your AsyncSSH listener, and at least on Linux it seems like connections are automatically spread across your multiple processes when you do that. So, up front you’d fork off some number of processes based on available cores on your system and then for each child process you’d do something similar to the simple server link I listed above, but with reuse_port=True added.

One caveat to this: With I tried this on macOS, it seemed to always prefer the first listener you set up. Other processes can listen on the same port, but until the first listener is closed the other listeners won’t get any connections. So, if you need this to work on macOS, a different approach might be required where you pass an accepted socket between processes. There’s no portable way to do that in Python, though. Anything you did there would be end up being OS-specific, if it was possible at all.

0reactions
ghostbodycommented, Sep 8, 2022

That’s awesome, man!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ServerSocket (Java Platform SE 7 ) - Oracle Help Center
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on...
Read more >
accept function of Python socket class - Pythontic.com
The accept () method of Python's socket class, accepts an incoming connection request from a TCP client. The accept() method is called on...
Read more >
The accept method of the ServerSocket class - java
In order to accept an incoming client connection you must call the ServerSocket.accept() method. ... Socket s=new Socket("SERVER'S IP",9000);.
Read more >
SSC1 - sockets
Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes -...
Read more >
Learn Java Programming - SocketServer and Socket Class ...
The ServerSocket and Socket class work hand-in-hand for providing communication links over TCP/IP networks. In order to follow along with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found