Control stript which is runing in background.

jak nospam at please.ty
Thu Dec 31 21:43:43 EST 2020


Il 01/01/2021 01:43, Cameron Simpson ha scritto:
> On 01Jan2021 01:21, jak <nospam at please.ty> wrote:
>> Il 01/01/2021 00:58, 2QdxY4RzWzUUiLuE at potatochowder.com ha scritto:
>>> Most of the time, I have several shells open, often with their own
>>> background jobs running.  Limiting write permission on the pipe to "me"
>>> wouldn't prevent concurrent access.
>>
>> This is true but there would be no difference if this happened through
>> a socket.
> 
> Accessing a socket makes a distinct separate data connection - other
> openers do not conflict with it. That's the critical difference between
> a socket and a pipe in terms of functionality, and why sockets have a
> connect/accept step.
> 
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> 

Maybe the fact that I'm not English and I don't know the language well
doesn't allow me to express myself clearly. Try it one more time:
The OP would like to give some command to a script that is running. How?
With a script that sends commands to it. One of the ways, as mentioned,
is by using a mini socket server. Given the needs of the OP and the fact
that sockets are a limited resource in a system, I took the liberty of
proposing a simple alternative: using a named pipe, also because, IMO,
sockets, in this case, are an overkill. with a few lines of code in a
thread in the running script they can allow it to receive commands:
#-----------------
import os, errno

fnpipe = 'cmdpipe'

try:
     os.mkfifo(fnpipe)
except OSError as e:
     if e.errno != errno.EEXIST:
         raise
while True:
     with open(fnpipe, 'rt', 1) as fifo:
         for line in fifo:
             print(line, ends='')
#-----------------

Running the command:

$ cat bible.txt > cmdpipe & cat bible.txt > cmdpipe & cat bible.txt > 
cmdpipe

the three texts do not mix. IMO, the OP should be enough. after that, I
know that a pipe is more like a queue than a soket and in this case a
socket, IMO, is wasted.

greetings, hoping to have been clearer than before.


More information about the Python-list mailing list