Python Forum
How to create waiting process?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create waiting process?
#1
Hi lets think i need to process image and i have function for it. How to create from this function process that will be always running and waiting for images from main script ?

My main script is busy with other things and i dont want to start always a new process. I just want to sent images to already running process and just get result from it. Is it possible?
I can not find any working example on internet.
Reply
#2
It's possible, but I'm not sure I understand what problem you're trying to solve. In many cases, starting up a new process is much less complex (and is just as performant) as handing off to an already running daemon.

What problem are you trying to avoid by not just firing off a new process?
Reply
#3
(Sep-02-2021, 04:24 PM)bowlofred Wrote: It's possible, but I'm not sure I understand what problem you're trying to solve. In many cases, starting up a new process is much less complex (and is just as performant) as handing off to an already running daemon.

What problem are you trying to avoid by not just firing off a new process?

Hi, Thank you for reply, exactly i have two Neural networks, and frames from webcam capture. I need to process these frames with both neural networks. I dont have time to do it serially. Let me show you example.

I need process that will be waiting for image.

this is what i have right now (just example),

for image in capture:
    #This take some time
    a = network1(image)
    #This also take some time
    b = network2(image)
    
And this is what i want.

By some way I want to have always running process that will wait for image and process it.
I need to process a and b concurently. But it is stupid idea to start new process every loop....

#"THIS IS MY ALWAYS RUNING PROCESS" 
def process(image):
    a = network1(image)
    
    return a 


for image in capture:
    
    #Now just by some way sent image to process
    a = sent image to process
    
    # And b will work on main process normally
    b = network2(image)
    
Reply
#4
If you want to start a process and send it a request to do something, there are multiple ways to do that:

- interprocess communication (IPC) mechanisms provided by your OS (e.g. FIFOs on UNIX)
- message queues (e.g. RabbitMQ, ZeroMQ)
- others (e.g. writing a web app that you send HTTP requests, ...)
samuelbachorik likes this post
Reply
#5
for image in capture:
    #This take some time
    a = network1(image)
    #This also take some time
    b = network2(image)
This doesn't directly spawn a new process. It waits for the data from each to return serially. What you could do instead is run it as a subprocess. Then the question becomes how you want to transfer the data between the processes. You could use pipes (stdin/stdout), or you could just have the subprocess read from a file and write it back out.

The advantage to this is you don't have to manage some daemon process that could fail. IPC can be simpler.

One way to do this is to imagine the processors just use the filesystem for I/O. We'd pass in the name of the file to process and the location of the output. Maybe something like this:

import subprocess
from pathlib import Path
cmd = "processor.py"
image_file = Path("path", "to", "image.img")

proc_a = subprocess.Popen(["python3", cmd, "--style", "network1", "--input", image_file], stdout=subprocess.PIPE)
proc_b = subprocess.Popen(["python3", cmd, "--style", "network2", "--input", image_file], stdout=subprocess.PIPE)

# image is processing here.  Can do some work that doesn't depend on the output

# now I need both outputs.  Wait for output to tell us the processing is done
output_a = proc_a.communicate()
output_b = proc_b.communicate()

# both images are done processing.  Read the output files to use.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,098 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,068 Nov-28-2023, 06:55 PM
Last Post: walker
  Create csv file with 4 columns for process mining thomaskissas33 3 761 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Waiting for heavy functions question philipbergwerf 14 3,385 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  Waiting and listening test 2 2,156 Nov-13-2020, 04:43 PM
Last Post: michael1789
  waiting for barcode scanner output, while main program continues to run lightframe109 3 4,665 Sep-03-2020, 02:19 PM
Last Post: DeaD_EyE
  waiting to connect Skaperen 9 3,587 Aug-17-2020, 05:58 AM
Last Post: Skaperen
  Launch another python command without waiting for a return. SpongeB0B 13 10,996 Jun-18-2020, 10:45 AM
Last Post: Yoriz
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,679 Sep-03-2019, 09:49 PM
Last Post: woooee
  waiting for many processes in parallel Skaperen 2 1,906 Sep-02-2019, 02:20 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020