Re: Calling pselect/ppoll/epoll_pwait

2022-12-13 Thread Ian Pilcher

On 12/2/22 14:00, Ian Pilcher wrote:

Does Python provide any way to call the "p" variants of the I/O
multiplexing functions?


Just to close this out ...

As others suggested, there's no easy way to call the "p" variants of the
I/O multiplexing functions, but this can be worked around by "mapping"
signals to file descriptors.

There are a few ways to accomplish this.

1. Use a Linux signalfd.  There's at least one library out there that
   provides signalfd support to Python.

2. Use signal.set_wakeup_fd()[1].  I didn't really explore this, as it
   appears that there isn't any way to filter the signals that will be
   reported.

3. Roll your own.  This turned out to be really simple for my use case,
   which is simply to set an exit flag and wake my program up if it
   receives SIGINT or SIGTERM.

   _sel = selectors.DefaultSelector()
   _exit_flag = False
   _sig_pipe_r, _sig_pipe_w = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC)

   def _sig_handler(signum, frame):
   global _exit_flag
   _exit_flag = True
   os.write(_sig_pipe_w, b'\x00')

   _sel.register(_sig_pipe_r, selectors.EVENT_READ)
   # register other file descriptors of interest
   signal.signal(signal.SIGINT, _sig_handler)
   signal.signal(signal.SIGTERM, _sig_handler)
   while not _exit_flag:
   ready = _sel.select()
   # handle other file descriptors

[1] https://docs.python.org/3/library/signal.html#signal.set_wakeup_fd

--

Google  Where SkyNet meets Idiocracy


--
https://mail.python.org/mailman/listinfo/python-list


Re: Calling pselect/ppoll/epoll_pwait

2022-12-04 Thread Barry
 On 3 Dec 2022, at 13:13, Weatherby,Gerard  wrote:

 

 Signalfd and select could probably be made to work if one codes around
 the race conditions pselect is provided to avoid. I’m not sure if using
 the GIL (for CPython) is sufficient or if a dedicated concurrency
 control (e.g. lock, mutex, semaphore) is necessary.

 An alternative is to call the C library function via a wrapper. I had
 need to use setfsuid on a project a couple of years ago and found this
 example:

 [1]https://gist.github.com/atdt/ebafa299e843a767139b

  

   I read this on SO when researching your question.
   Search for epoll and signal.

   (I would post the link but ipad turns the link into an image…)

   I assume that the lack of pepoll means you can use epoll and

   signalfd without race conditions. The trick seems to be setting

   the signal to ignore.

   Barry

  

 From: Python-list 
 on behalf of Barry 

 Date: Friday, December 2, 2022 at 7:02 PM
 To: Ian Pilcher 
 Cc: python-list@python.org 
 Subject: Re: Calling pselect/ppoll/epoll_pwait

 *** Attention: This is an external email. Use caution responding,
 opening attachments or clicking on links. ***

 > On 2 Dec 2022, at 20:03, Ian Pilcher  wrote:
 >
 > Does Python provide any way to call the "p" variants of the I/O
 > multiplexing functions?
 >
 > Looking at the documentation of the select[1] and selectors[2]
 modules,
 > it appears that they expose only the "non-p" variants.
 >
 > [1]
 
[2]https://urldefense.com/v3/__https://docs.python.org/3/library/select.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_OD5qBjeA$
 > [2]
 
[3]https://urldefense.com/v3/__https://docs.python.org/3/library/selectors.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_MwNjgO8A$

 Can you use signalfd and select/poll/epoll?

 Barry

 >
 > --
 >
 
 > Google  Where SkyNet meets
 Idiocracy
 >
 
 > --
 >
 
[4]https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$
 >

 --
 
[5]https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$

References

   Visible links
   1. https://gist.github.com/atdt/ebafa299e843a767139b
   2. 
https://urldefense.com/v3/__https:/docs.python.org/3/library/select.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_OD5qBjeA$
   3. 
https://urldefense.com/v3/__https:/docs.python.org/3/library/selectors.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_MwNjgO8A$
   4. 
https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$
   5. 
https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling pselect/ppoll/epoll_pwait

2022-12-03 Thread Weatherby,Gerard
Signalfd and select could probably be made to work if one codes around the race 
conditions pselect is provided to avoid. I’m not sure if using the GIL (for 
CPython) is sufficient or if a dedicated concurrency control (e.g. lock, mutex, 
semaphore) is necessary.


An alternative is to call the C library function via a wrapper. I had need to 
use setfsuid on a project a couple of years ago and found this example:

https://gist.github.com/atdt/ebafa299e843a767139b



From: Python-list  on 
behalf of Barry 
Date: Friday, December 2, 2022 at 7:02 PM
To: Ian Pilcher 
Cc: python-list@python.org 
Subject: Re: Calling pselect/ppoll/epoll_pwait
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

> On 2 Dec 2022, at 20:03, Ian Pilcher  wrote:
>
> Does Python provide any way to call the "p" variants of the I/O
> multiplexing functions?
>
> Looking at the documentation of the select[1] and selectors[2] modules,
> it appears that they expose only the "non-p" variants.
>
> [1] 
> https://urldefense.com/v3/__https://docs.python.org/3/library/select.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_OD5qBjeA$<https://urldefense.com/v3/__https:/docs.python.org/3/library/select.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_OD5qBjeA$>
> [2] 
> https://urldefense.com/v3/__https://docs.python.org/3/library/selectors.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_MwNjgO8A$<https://urldefense.com/v3/__https:/docs.python.org/3/library/selectors.html__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_MwNjgO8A$>

Can you use signalfd and select/poll/epoll?

Barry

>
> --
> 
> Google  Where SkyNet meets Idiocracy
> 
> --
> https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$>
>

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!hClFFo4XdiwQAhHxDbHA5zFr490Of9uheHSf84V9cREMHyw1kX-baG5HzXWMt-hFLP30q6DpSUb2G_PhasKBfg$>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling pselect/ppoll/epoll_pwait

2022-12-02 Thread Barry


> On 2 Dec 2022, at 20:03, Ian Pilcher  wrote:
> 
> Does Python provide any way to call the "p" variants of the I/O
> multiplexing functions?
> 
> Looking at the documentation of the select[1] and selectors[2] modules,
> it appears that they expose only the "non-p" variants.
> 
> [1] https://docs.python.org/3/library/select.html
> [2] https://docs.python.org/3/library/selectors.html

Can you use signalfd and select/poll/epoll?

Barry

> 
> -- 
> 
> Google  Where SkyNet meets Idiocracy
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list