Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A person with one watch knows what time it is; a person with two watches is never sure. Proverb


devel / comp.lang.python / Re: Python child process in while True loop blocks parent

SubjectAuthor
o Re: Python child process in while True loop blocks parentJen Kris

1
Re: Python child process in while True loop blocks parent

<mailman.5.1638374503.15287.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16295&group=comp.lang.python#16295

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jenk...@tutanota.com (Jen Kris)
Newsgroups: comp.lang.python
Subject: Re: Python child process in while True loop blocks parent
Date: Wed, 1 Dec 2021 17:01:36 +0100 (CET)
Lines: 154
Message-ID: <mailman.5.1638374503.15287.python-list@python.org>
References: <MphOu5i--3-2@tutanota.com>
<B05343F2-30B5-470C-8CFC-EBFA7C5EFD6A@barrys-emacs.org>
<MphoqOf--3-2@tutanota.com>
<7A3CE5EB-C53A-404D-BF32-77CF0243BF3D@barrys-emacs.org>
<MpqihHm--3-2@tutanota.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de t6ITqfUpwd4e/QX1Cfd/oQsU1YD0l8ubp9BvZ9LjueeA==
Return-Path: <jenkris@tutanota.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tutanota.com header.i=@tutanota.com header.b=tY9m1XV8;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'yet.': 0.04; 'loop': 0.07; 'parent': 0.07; 'blocking':
0.09; 'cc:addr:python-list': 0.09; 'child': 0.09; 'flags': 0.09;
'ideas.': 0.09; 'set,': 0.09; 'subject:Python': 0.12; 'import':
0.15; 'url:mailman': 0.15; '>>>>': 0.16; 'affects': 0.16; 'barry':
0.16; 'cameron': 0.16; 'cc:name:python list': 0.16; 'default.':
0.16; 'events:': 0.16; 'interpreter': 0.16; 'length.': 0.16;
'looping': 0.16; 'pipe': 0.16; 'subject:True': 0.16;
'subject:child': 0.16; 'subject:loop': 0.16; 'subject:parent':
0.16; 'subject:while': 0.16; 'writes': 0.16; 'wrote:': 0.16;
'problem': 0.16; 'python': 0.16; 'uses': 0.19; 'libraries': 0.19;
'cc:addr:python.org': 0.20; 'code': 0.23; 'run': 0.23;
'received:de': 0.23; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25;
'url-ip:188.166/16': 0.25; 'perform': 0.26; 'bit': 0.27; '>>>':
0.28; 'email addr:python.org&gt;': 0.28; 'example,': 0.28; 'goes':
0.28; 'suggest': 0.28; 'it,': 0.29; 'comment': 0.31; 'putting':
0.31; 'url-ip:188/8': 0.31; 'program': 0.31; 'question': 0.32;
'assume': 0.32; 'end.': 0.32; 'python-list': 0.32; 'signal': 0.32;
'received:192.168.1': 0.32; 'but': 0.32; "i'm": 0.33; 'someone':
0.34; 'header:In-Reply-To:1': 0.34; 'running': 0.34; 'functions':
0.36; 'source': 0.36; 'received:192.168': 0.37; 'way': 0.38;
'could': 0.38; 'put': 0.38; 'read': 0.38; '8bit%:14': 0.38;
'thanks': 0.38; 'handle': 0.39; 'setting': 0.39; 'use': 0.39;
'appears': 0.40; 'case.': 0.40; 'program.': 0.40; 'should': 0.40;
'event': 0.40; 'best': 0.61; 'today': 0.61; 'detail': 0.61;
'likely': 0.61; 'here.': 0.61; 'skip:o 10': 0.61; 'skip:o 20':
0.63; 'true': 0.63; 'once': 0.63; 'events': 0.63; 'lock': 0.64;
'your': 0.64; 'plan': 0.64; 'process.': 0.65; 'similar': 0.65;
'well': 0.65; 'named': 0.65; 'skip:e 20': 0.67; 'right': 0.68;
'skip:# 10': 0.68; ':)\xc2\xa0': 0.69; 'tip': 0.69; 'below': 0.69;
'them,': 0.70; 'global': 0.73; 'man': 0.74; 'process,': 0.75;
'communicates': 0.84; 'email name:&lt;python-list': 0.84;
'parent.': 0.84; 'pipes': 0.84; 'true:': 0.84; 'behind': 0.88;
'receives': 0.91; 'worry': 0.95
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1638374496;
s=s1; d=tutanota.com;
h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender;
bh=4+IBpNHjC94GmgasYFSSweMJpHbNY20WflVnuCV07MA=;
b=tY9m1XV8fdf0OUfPSJaPszuqG/bLsL8g/skqjQcLLZVxyFHPB2g7F5cmBX3QtIja
BjrX/yciQmllWzVZwEwfA1MsPxiYeKM6C5/GIaEF6C9HgceZ8+HdQk9cIqDuxObwcfv
q0MWOJmXwRbnYcn+u+aIT5nqrA0COO4kSnjQtCDlI1OHk0O23ByjIZffkexbDLkOQZy
66on4Nvsf9P6Ud5ymz9LvLR+2ynN3lgG4zvOsEN2MnJl2MwaMKR5zCkmfpMWbW6PynP
zVdgfMUaOLkv+c76tBsq7vUhqsJ7HbaovCBlb56H9iFFGULjS2YVyiJSt5PEo15jJpl
/g72UN+NSA==
In-Reply-To: <7A3CE5EB-C53A-404D-BF32-77CF0243BF3D@barrys-emacs.org>
X-Content-Filtered-By: Mailman/MimeDel 2.1.38
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.38
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <MpqihHm--3-2@tutanota.com>
X-Mailman-Original-References: <MphOu5i--3-2@tutanota.com>
<B05343F2-30B5-470C-8CFC-EBFA7C5EFD6A@barrys-emacs.org>
<MphoqOf--3-2@tutanota.com>
<7A3CE5EB-C53A-404D-BF32-77CF0243BF3D@barrys-emacs.org>
 by: Jen Kris - Wed, 1 Dec 2021 16:01 UTC

Thanks for your comment re blocking. 

I removed pipes from the Python and C programs to see if it blocks without them, and it does.  It looks now like the problem is not pipes.  I use fork() and execv() in C to run Python in a child process, but the Python process blocks because fork() does not create a new thread, so the Python global interpreter lock (GIL) prevents the C program from running once Python starts.  So the solution appears to be run Python in a separate thread, which I can do with pthread create.  See "Thread State and the Global Interpreter Lock" https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock and the sections below that "Non-Python created threads" and "Cautions about fork()." 

I'm working on that today and I hope all goes well :) 

Nov 30, 2021, 11:42 by barry@barrys-emacs.org:

>
>
>
>> On 29 Nov 2021, at 22:31, Jen Kris <>> jenkris@tutanota.com>> > wrote:
>>
>> Thanks to you and Cameron for your replies.  The C side has an epoll_ctl set, but no event loop to handle it yet.  I'm putting that in now with a pipe write in Python-- as Cameron pointed out that is the likely source of blocking on C.  The pipes are opened as rdwr in Python because that's nonblocking by default.  The child will become more complex, but not in a way that affects polling.  And thanks for the tip about the c-string termination. 
>>
>>
>
> flags is a bit mask. You say its BLOCKing by not setting os.O_NONBLOCK.
> You should not use O_RDWR when you only need O_RDONLY access or only O_WRONLY access.
>
> You may find
>
> man 2 open
>
> useful to understand in detail what is behind os.open().
>
> Barry
>
>
>
>
>>
>>
>> Nov 29, 2021, 14:12 by >> barry@barrys-emacs.org>> :
>>
>>>
>>>
>>>> On 29 Nov 2021, at 20:36, Jen Kris via Python-list <>>>> python-list@python.org>>>> > wrote:
>>>>
>>>> I have a C program that forks to create a child process and uses execv to call a Python program. The Python program communicates with the parent process (in C) through a FIFO pipe monitored with epoll().
>>>>
>>>> The Python child process is in a while True loop, which is intended to keep it running while the parent process proceeds, and perform functions for the C program only at intervals when the parent sends data to the child -- similar to a daemon process.
>>>>
>>>> The C process writes to its end of the pipe and the child process reads it, but then the child process continues to loop, thereby blocking the parent.
>>>>
>>>> This is the Python code:
>>>>
>>>> #!/usr/bin/python3
>>>> import os
>>>> import select
>>>>
>>>> #Open the named pipes
>>>> pr = os.open('/tmp/Pipe_01', os.O_RDWR)
>>>>
>>> Why open rdwr if you are only going to read the pipe?
>>>
>>>> pw = os.open('/tmp/Pipe_02', os.O_RDWR)
>>>>
>>> Only need to open for write.
>>>
>>>>
>>>> ep = select.epoll(-1)
>>>> ep.register(pr, select.EPOLLIN)
>>>>
>>>
>>> Is the only thing that the child does this:
>>> 1. Read message from pr
>>> 2. Process message
>>> 3. Write result to pw.
>>> 4. Loop from 1
>>>
>>> If so as Cameron said you do not need to worry about the poll.
>>> Do you plan for the child to become more complex?
>>>
>>>>
>>>> while True:
>>>>
>>>> events = ep.poll(timeout=2.5, maxevents=-1)
>>>> #events = ep.poll(timeout=None, maxevents=-1)
>>>>
>>>> print("child is looping")
>>>>
>>>> for fileno, event in events:
>>>> print("Python fileno")
>>>> print(fileno)
>>>> print("Python event")
>>>> print(event)
>>>> v = os.read(pr,64)
>>>> print("Pipe value")
>>>> print(v)
>>>>
>>>> The child process correctly receives the signal from ep.poll and correctly reads the data in the pipe, but then it continues looping. For example, when I put in a timeout:
>>>>
>>>> child is looping
>>>> Python fileno
>>>> 4
>>>> Python event
>>>> 1
>>>> Pipe value
>>>> b'10\x00'
>>>>
>>> The C code does not need to write a 0 bytes at the end.
>>> I assume the 0 is from the end of a C string.
>>> UDS messages have a length.
>>> In the C just write 2 byes in the case.
>>>
>>> Barry
>>>
>>>> child is looping
>>>> child is looping
>>>>
>>>> That suggests that a while True loop is not the right thing to do in this case. My question is, what type of process loop is best for this situation? The multiprocessing, asyncio and subprocess libraries are very extensive, and it would help if someone could suggest the best alternative for what I am doing here.
>>>>
>>>> Thanks very much for any ideas.
>>>>
>>>>
>>>> --
>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>
>>
>>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor