change file descriptor for socket in python
I'm trying to manually create the file descriptor associated with a socket in python and then loaded directly into memory with mmap. Create a file into memory with mmap is simple,开发者_开发百科 but I can not find a way to associate the file with a socket.
Thanks for your responses.
The problem I have is I can not make more of a number of sockets for python (or operating system) I get the error: "[errno 24] Too many open files."
I think the error is because I can not create more file descriptors on disk, so I want to create them in memory. To avoid this limitation.
Any suggestions?
Why do you want to load this into memory using mmap? If you are on a unix variant, you can create a unix socket which is a file descriptor which can be used just like any other socket. A socket and a memory-mapped file are two distinct entities - it is probably not a good idea to try and mix them.
Perhaps it would be helpful to take a step back and discuss what you are trying to do at a higher level.
There is a good chance that I am royally misinterpreting your question. Are you saying that there is an existing socket file for which you would like to create a Python socket object? If so, socket.fromfd()
will duplicate the fd and create a socket object.
http://docs.python.org/library/socket.html#socket.fromfd
edit to address Dani's post
I think that you misunderstand how file descriptors work. There is a limit set by the OS. This has nothing to do with what the FDs point to, so mmap will not help you here (sockets aren't on disk either btw). You probably just need to do better file management - close files when you are done with them.
In case if you just have really demanding requirements, you may need to increase the limit on open FDs. This blog post has an excellent example of using the resource
module (*NIX-only) to get and set the open file limit. The getrlimit man page has more information on its use.
精彩评论