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

Add a new optional timeout parameter to socket.socket() constructor #64012

Closed
vstinner opened this issue Nov 27, 2013 · 6 comments
Closed

Add a new optional timeout parameter to socket.socket() constructor #64012

vstinner opened this issue Nov 27, 2013 · 6 comments
Labels
type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

BPO 19813
Nosy @gvanrossum, @pitrou, @vstinner

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-11-28.19:52:23.985>
created_at = <Date 2013-11-27.11:57:08.263>
labels = ['type-feature']
title = 'Add a new optional timeout parameter to socket.socket() constructor'
updated_at = <Date 2013-11-28.21:11:54.455>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2013-11-28.21:11:54.455>
actor = 'vstinner'
assignee = 'none'
closed = True
closed_date = <Date 2013-11-28.19:52:23.985>
closer = 'gvanrossum'
components = []
creation = <Date 2013-11-27.11:57:08.263>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 19813
keywords = []
message_count = 6.0
messages = ['204575', '204576', '204579', '204690', '204694', '204699']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'pitrou', 'vstinner', 'neologix']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue19813'
versions = ['Python 3.5']

@vstinner
Copy link
Member Author

Since Linux 2.6.28, socket() syscall accepts a new SOCK_NONBLOCK flag in the socket type. It avoids 1 or 2 extra syscalls to set the socket in non-blocking mode. This flag comes also slowly in other operating systems: NetBSD, FreeBSD, etc.

FreeBSD:
http://lists.freebsd.org/pipermail/freebsd-hackers/2013-March/042242.html

Discussion in the POSIX standard:
http://austingroupbugs.net/view.php?id=411

Avoiding the syscalls has been proposed on python-dev a few months ago:
https://mail.python.org/pipermail/python-dev/2013-January/123661.html

I tried to include SOCK_NONBLOCK in my PEP-446, but I have been asked to treat it separatly because it's completly different than O_CLOEXEC (even it looks similar).
http://hg.python.org/peps/file/fa873d5aed27/pep-0446.txt#l64

I also proposed to add 2 functions in the PEP:

  • os.get_blocking(fd:int) -> bool
  • os.set_blocking(fd:int, blocking: bool)

I propose to add a new timeout parameter to socket.socket() constructor which would set the timeout internal attribute and set O_NONBLOCK flag using the new SOCK_NONBLOCK flag, ioctl() or fcntl() (depending on the OS and on what is available).

I don't know if something special should be done on Windows for non-blocking sockets? socket.socket.setblocking() already exists and I suppose that it works on Windows :-) See also the discussion in my old PEP for non-blocking operations in files on Windows:
http://hg.python.org/peps/file/fa873d5aed27/pep-0446.txt#l177

I don't know if the new parameter can just be added at the end of the parameter list, or it should be a keyword-only parameter to avoid breakpoint backward compatibility?

socket.socketpair() and socket.socket.dup() and socket.fromfd(), socket.create_connection() (and more generally any function creating a new socket) may also be modified.

--

By the way, internal_setblocking() currently uses 2 fcntl() syscalls on Linux, whereas it could be implemented with a single ioctl() syscall using FIONBIO.

Set O_NONBLOCK flag:

int flag = 1;
ioctl(fd, FIONBIO, &flag);

Clear O_NONBLOCK flag:

int flag = 0;
ioctl(fd, FIONBIO, &flag);

Tell me if you prefer a different issue for this optimization.

@vstinner vstinner added the type-feature A feature request or enhancement label Nov 27, 2013
@vstinner
Copy link
Member Author

Note: Python supports socket.SOCK_NONBLOCK since Python 3.2 (issue bpo-7523).

@pitrou
Copy link
Member

pitrou commented Nov 27, 2013

This really sounds pointless to me. How many sockets do you create per second?

@neologix
Copy link
Mannequin

neologix mannequin commented Nov 28, 2013

I'm with Antoine, this is *really* going too far.
SOCK_CLOEXEC and friends are useful to avoid race conditions: there's
no such concern with the non-blocking flag.
Shaving one or two syscalls is IMO completely useless, and doesn't
justify the extra code and, more importantly, clutter in the
constructor.
I'm -10.

@gvanrossum
Copy link
Member

OK, let's forget about it.

@vstinner
Copy link
Member Author

I don't see what I can do against a -10 vote! :-)

I opened the issue bpo-19827 for the simple syscall optimization.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants