Control stript which is runing in background.

jak nospam at please.ty
Thu Dec 31 12:07:31 EST 2020


Il 31/12/2020 11:43, Petro ha scritto:
> Hi.
> I would like to make something like this:
> A python script would run headlessly in the background.
> I would like to control the script from the command line using other python scripts or from the python shell.
>  From time to time I would ask the main script to create a popup window with an image or a plot.
> What would be the proper way to approach it. How to make communication between two scripts?
> Thank you.
> Petro.
> 

using named pipes would be an alternative. A small example that produces 
an echo for windows. For linux it's simpler using os.mkfifo:

# ---------------------
import win32pipe as wp, win32file as wf

pfile = r'\\.\pipe\mypipe'
how = 0
data: bytes
while True:
     pipe = wp.CreateNamedPipe(pfile, wp.PIPE_ACCESS_DUPLEX,
                               wp.PIPE_TYPE_BYTE | wp.PIPE_READMODE_BYTE 
| wp.PIPE_WAIT,
                               wp.PIPE_UNLIMITED_INSTANCES, 0xffff, 
0xffff, 0, None)
     if pipe:
         wp.ConnectNamedPipe(pipe, None)
         while True:
             try:
                 rv, data = wf.ReadFile(pipe, 1024)
             except:
                 wf.CloseHandle(pipe)
                 break
             else:
                 print(data.decode(), end='')
     else:
         break
# ---------------------

you can pass commands to it this way too:

$> echo "print log" > \\.\pipe\mypipe


More information about the Python-list mailing list