Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:25:28+00:00 2026-06-06T17:25:28+00:00

I’m trying to watch a process and wait for a certain pattern, say: open

  • 0

I’m trying to watch a process and wait for a certain pattern, say:

open someFile id=123

then, after that, I want to wait for

close id=123

I tried to write the script as follows:

running_procs = [Popen(["process", "and", "options"], stdout=PIPE, stderr=PIPE)]

while running_procs:
    for proc in running_procs:
        retcode = proc.poll()
        if retcode is not None: # Process finished.
            running_procs.remove(proc)
            break
        else:
            while True:
                next_line = proc.stdout.readline()
                if next_line == '' and proc.poll() != None:
                    break
                m = re.search( r'someFile.*id\=([0-9]*)', next_line, re.M|re.I)
                if m:
                  print m.group(1)

But it seems to be performing way too slow. Any suggestions on handling a lot of lines in a pipe? Is there a faster way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:25:30+00:00Added an answer on June 6, 2026 at 5:25 pm

    There is nothing in this specific example to indicate that it should be slow simply on account of the code. With only a single process in your list, its going to read lines as fast as the process makes them available. This means the code is going to be dependent on the subprocess flushing its output and making lines available. But thats really to be expected.

    Since you are going to always be reading line by line, you might want to set bufsize=1 in your Popen, to make sure its always line buffered:

    Popen(["process"], stdout=PIPE, stderr=PIPE, bufsize=1)
    

    I do however see an issue if you intend to be running multiple processes, as indicated by the fact that you are using a list of possible processes and popping dead ones from them. Your loop is going to block on one process at a time. If this is not your intention to have the processes read serially, then it will be a cause of a slow down in how you get your data back. They run in parallel, but only be monitored serially.

    Aside from this, you will have to go into more detail about why you consider the results to be slow and what you expect to happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.