473,658 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ulimit on open sockets ?

Hi,

I've written this code, the general idea was to listen on all 65535
port of tcp for connection.
"""
#!/usr/bin/env python

import socket, select

def get_non_blockin g_socket(port_n umber):
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setblocking(0 )
s.bind(('0.0.0. 0', port_number))
s.listen(1)
return s

all_sockets = map(get_non_blo cking_socket, xrange(10000, 15000))

while 1:
ready_to_read, ready_to_write, in_error =
select.select(a ll_sockets, [], [], 0)
for nb_active_socke t in all_sockets:
if nb_active_socke t in ready_to_read:
conn, addr = nb_active_socke t.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
"""

The thing is that when I tried to run this at first I got
"""
python non_blocking_ra nge.py
Traceback (most recent call last):
File "non_blocking_r ange.py", line 12, in ?
all_sockets = map(get_non_blo cking_socket, xrange(10000, 15000))
File "non_blocking_r ange.py", line 6, in get_non_blockin g_socket
s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
File "/usr/lib/python2.4/socket.py", line 148, in __init__
_sock = _realsocket(fam ily, type, proto)
socket.error: (24, 'Too many open files')
"""

So I set ulimit -n 500000, now I'm getting
"""
python non_blocking_ra nge.py
Traceback (most recent call last):
File "non_blocking_r ange.py", line 15, in ?
ready_to_read, ready_to_write, in_error =
select.select(a ll_sockets, [], [], 0)
ValueError: filedescriptor out of range in select()
"""

Should I be using a different version of select or something? Or
should I implement this the other way around, if so please suggest
how.

Thank you very much,
(enthusiastical ly learning python) Maxim.

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 9 '07 #1
6 7029
Maxim Veksler wrote:
I've written this code, the general idea was to listen on all
65535 port of tcp for connection.
Please excuse the question: Why would anyone want to do such a manic
thing (instead of, e. g., using raw sockets)?

Regards,
Björn

--
BOFH excuse #326:

We need a licensed electrician to replace the light bulbs in the
computer room.

Apr 10 '07 #2
Maxim Veksler <hq*****@gmail. comwrote:
ValueError: filedescriptor out of range in select()
"""

Should I be using a different version of select or something? Or
select typically supports 1024 FDs at most (a design limit of the
underlying operating system). You may want to try poll instead (epoll
might be better but I doubt Python supports it yet).
Alex
Apr 10 '07 #3
On 4/10/07, Alex Martelli <al***@mac.comw rote:
Maxim Veksler <hq*****@gmail. comwrote:
ValueError: filedescriptor out of range in select()
"""

Should I be using a different version of select or something? Or

select typically supports 1024 FDs at most (a design limit of the
underlying operating system). You may want to try poll instead (epoll
might be better but I doubt Python supports it yet).
I've read some post the other day of a guy faced similar problem and
it turns out {e,}poll is limited as well, besides I don't know how to
use it so an example would be great.

Now, someone I work with suggested a simple work around "Pass the list
objects in groups of 1024 each time to the select.select structure". I
think it's acceptable and good advice, the thing is I don't know how
to implement this "the python way" (that is - with out it being ugly).

Can I do modulation ( % 1024 ) on the main iterator loop?
Something like:

for nb_active_socke t in (all_sockets % 1024):
if nb_active_socke t in ready_to_read:
conn, addr = nb_active_socke t.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

?

Thanks for helping,
Maxim.
>
Alex
--
http://mail.python.org/mailman/listinfo/python-list

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 12 '07 #4

On Apr 12, 2007, at 1:17 PM, Maxim Veksler wrote:
...
Now, someone I work with suggested a simple work around "Pass the list
objects in groups of 1024 each time to the select.select structure". I
think it's acceptable and good advice, the thing is I don't know how
to implement this "the python way" (that is - with out it being ugly).
I don't understand how you're going to make it work (I see no select
calls in your code and I don't understand how you'd get one in there
by polling), but I'm going to just explain how to get slices of 1024
items at a time from a long list.

Simplest way:

for i in xrange(0, len(longlist), 1024):
shortlist = longlist[i:i+1024]
# rest of the body goes here

More elegant/reusable:

def sliceby(longlis t, N=1024):
for i in xrange(0, len(longlist), 1024):
yield longlist[i:i+1024]

for shortlist in sliceby(longlis t):
# body goes here

If you want to show off, itertools.group by may be suitable for that:

for _, g in itertools.group by(enumerate(lo nglist), lambda (i, j): i//
1024):
shortlist = list(a for b, a in g)
# rest of the body goes here

but I wouldn't recommend it in this case for other purposes.
Alex

Apr 13 '07 #5
On 4/13/07, Alex Martelli <al***@mac.comw rote:
>
On Apr 12, 2007, at 1:17 PM, Maxim Veksler wrote:
...
Now, someone I work with suggested a simple work around "Pass the list
objects in groups of 1024 each time to the select.select structure". I
think it's acceptable and good advice, the thing is I don't know how
to implement this "the python way" (that is - with out it being ugly).

I don't understand how you're going to make it work (I see no select
calls in your code and I don't understand how you'd get one in there
by polling), but I'm going to just explain how to get slices of 1024
items at a time from a long list.
Thank you. I'm attaching the full code so far for reference, sadly it
still doesn't work. It seems that select.select gets it's count of
fd's not from the amount passed to it by the sub_list but from the
kernel (or whatever) count for the process; The main issue here is
that it seems I won't be able to use select for the simple
non-blocking process and am forced to check poll to see if that helps.

The error I'm getting is still the same:

# ulimit -n
500000
# python listener_socket s_range.py
Traceback (most recent call last):
File "listener_socke ts_range.py", line 22, in ?
ready_to_read, ready_to_write, in_error =
select.select(s elect_cap_socke ts, [], [], 0)
ValueError: filedescriptor out of range in select()
"""
#!/usr/bin/env python

import socket, select

def get_non_blockin g_socket(port_n umber):
print port_number

s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setblocking(0 )
s.bind(('0.0.0. 0', port_number))
s.listen(1)
return s

def slice_by_fd_lim it(longlist, N=1024):
for i in xrange(0, len(longlist), N):
yield longlist[i:i+N]

all_sockets = map(get_non_blo cking_socket, xrange(10000, 20000))

while 1:
for select_cap_sock ets in slice_by_fd_lim it(all_sockets) :
ready_to_read, ready_to_write, in_error =
select.select(s elect_cap_socke ts, [], [], 0)
for nb_active_socke t in all_sockets:
if nb_active_socke t in ready_to_read:
conn, addr = nb_active_socke t.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
"""

--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?
Apr 14 '07 #6
Maxim Veksler <hq*****@gmail. comwrote:
...
Thank you. I'm attaching the full code so far for reference, sadly it
still doesn't work. It seems that select.select gets it's count of
fd's not from the amount passed to it by the sub_list but from the
kernel (or whatever) count for the process; The main issue here is
It's not a problem of COUNT of FD's, i.e., how many you're passing to
select; the problem is the value of the _highest_ number you can pass.
It's an API-level limitation, not an issue with Python per se: the
select API takes a "bit vector" of N bits, representing a set of FDs in
that way, and N is fixed at kernel-compilation time (normally to 1024).

The poll system call does not have this particular limitation, which is
why select.poll may be better for you.

Moreover, your code has other performance problems:

while 1:
for select_cap_sock ets in slice_by_fd_lim it(all_sockets) :
ready_to_read, ready_to_write, in_error =
select.select(s elect_cap_socke ts, [], [], 0)
for nb_active_socke t in all_sockets:
if nb_active_socke t in ready_to_read:
A small issue is with the last two lines -- instead of looping directly
on the small "ready-to-read" list, you're looping on the large
all_sockets one and looking each up in the small list -- that's just
throwing performance out of the window, and adding complexity, for no
benefit whatsoever.

The big issue is that you are "ceaselessl y polling". If no socket is
ready to read, you force select to return immediately anyway, and
basically call select at once afterwards. You churn on the CPU without
surcease, using 100% of it, hogging it for this "busy wait", possibly to
the point of crowding out the kernel from some of the CPU time it needs
to do useful work in the TCP-IP stack. Busy-wait is a bad thing...
never call select with a timeout of 0 in a tight loop. This
recommendation also applies to the polling-object that you can build
with select.poll, and any other situation where you're waiting for
another thread or process to deliver some data -- ideally you should
wait in a blocking way, if that's unfeasible at least make sure you're
letting some time pass between such calls, by using small but non-0
timeout (or even by inserting calls to time.sleep if that's what it
takes).

The risk of such "antipatter ns" is a good reason why it would be better
to use a well-designed, well-coded, well-debugged existing framework,
such as Twisted, rather than roll your own, btw. With twisted, you can
choose among many appropriate implementations of "reactor" (the key
design pattern for async prorgramming) and activate the one that is most
suitable for your needs (including, e.g., one based on epoll, which
gives better performance than poll on suitable operating systems).

If you're adamant on "rolling your own", though, you can find a Python
epoll module at <http://cheeseshop.pyth on.org/pypi/pyepoll/0.2(it's
said to be in alpha status, though; I believe there are other such
modules around, but pyepoll seems to be the only one on Cheese Shop).
Alex
Apr 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1695
by: Chris | last post by:
Hi All, I am trying to write a script to check the HTTP response code for a largish database of URLs (some 12,000 sites). I have tried a couple of third party classes such as Snoopy and URLHelper which allow me to feed in a URL and should return the response header. The problem I am getting is that the script will not handle more than about 30 URLs. Any more than that an it just fails. Reading around a little suggests that I am...
3
2617
by: Anand Pillai | last post by:
I recently noted that urllib2.urlopen(...) for http:// urls does not make an explicit call to close the underlying HTTPConnection socket once the data from the socket is read. This might not be required since the garbage collector will close & collect open sockets that are not closed, but it might cause the system to run out of socket memory if there are multiple threads, each opening a socket and the gc not running in between.
14
1354
by: ed | last post by:
this script should create individual threads to scan a range of IP addresses, but it doesnt, it simple ... does nothing. it doesnt hang over anything, the thread is not being executed, any ideas anyone? ---------- import socket import threading import traceback
1
5410
by: gmiley | last post by:
I have looked around in System.Net{} and System.Net.Sockets{} namespaces, but cannot seem to find a collection, or method for obtaining an active connections collection. I guess I am basically looking for a way to produce similar output as what netstat would produce, but in a controled C# environment where those enumerated connections could be manipulated. Thanks for any help.
5
1717
by: j_macaroni | last post by:
I know Javascript has no methods to directly open sockets. Since that is not possible, I was wondering if anyone knows of a Java applet somewhere that does this that can can be instantiated in JS. This would be running on a Windows client under IE or Firefox. Thanks
3
1720
by: OneMustFall | last post by:
Reciently i wrote a simple client (in twisted) using Reconnecting Factory. That client logins to my socket server.. and that`s it. Interesting thing is that it is seems that twisted client, sends some ping on a TCP level without sending any data to the socket directly. Because when i pull out cord from the ethernet card simulating network falure, client in about 10-15 seconds determines that connection lost!! (pretty cool)
4
2761
by: Funke | last post by:
Assume that in C#, I create a server socket (listener) and code to start new threads with each connection using BeginAccept(). After some time, I have three threads running, each with their own client socket connection. If I close the listener socket, will the client sockets also shut down? Or do I need to manually shut these down as well?
5
2071
by: jman | last post by:
Is there a way to determine if a particular socket is open on the localhost? I'm looking for something that will either return a list of open sockets or a function that will take a port number and return some sort of status value, boolean or otherwise. I am trying to write an app that will determine if another application I wrote has started correctly and is listening for incoming requests. Thanks
7
2780
by: Nash | last post by:
Hi, I have a Client and Server application which does communication using sockets(APM). The problem is Whenever the client is connected to a server i am storing the socket in a list. When server wants to send a message to a particular client whose ip is known, i have to loop through all the sockets in the list, gets its ip and compare it with my ip and send the data. is there any other way to avoid looping the huge list??
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.