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 7012705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:17:26+00:00 2026-05-27T22:17:26+00:00

I need support of you in my little program, that creates a subprocess, here

  • 0

I need support of you in my little program, that creates a subprocess, here a 10 time ping, and should be killed by a given timeout value (5). This example does not succeed.
Can you give me a hint?

Regards.
Stefan

Output:

Traceback (most recent call last):
File "./nwcheck.py.work", line 146, in <module>
MyCheck().check().exit()
File "./nwcheck.py.work", line 80, in check
output = process.communicate()
File "/usr/lib/python2.6/subprocess.py", line 701, in communicate
return self._communicate(input)
File "/usr/lib/python2.6/subprocess.py", line 1199, in _communicate
rlist, wlist, xlist = select.select(read_set, write_set, [])
File "./nwcheck.py.work", line 29, in alarm_handler
raise alarm
TypeError: exceptions must be old-style classes or derived from BaseException, not   builtin_function_or_method

CODE:

def check(self):

    class Alarm(Exception):
        pass

    def alarm_handler(signum, frame):
        raise alarm

    def get_process_children(pid):
        p = Popen('ps --no-headers -o pid --ppid %d' % pid, shell = True,
                   stdout = PIPE, stderr = PIPE)
        stdout, stderr = p.communicate()
        return [int(p) for p in stdout.split()]
    timeout = 5
    args2 = [
            'ping',
            'localhost',
            '-c 10',
            ]
    process = subprocess.Popen(args2, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env={'LANG':'de_DE@euro'})
    processpid = process.pid
    print processpid
    if timeout != -1:
        signal(SIGALRM, alarm_handler)
        alarm(timeout)
        print 'in timeout abfrage'
    try:
        ## catch stdout and stderr
        output = process.communicate()
        if timeout != -1:
            alarm(0)
            print 'in timeout abfrage 2'
    except Alarm:
        pids = [process.pid]
        print pids
        if kill_tree:
            pids.extend(get_process_children(process.pid))
        for pid in pids:
        # process might have died before getting to this line
        # so wrap to avoid OSError: no such process
            try:
                kill(pid, SIGKILL)
            except OSError:
                pass
        return -9, '', ''

    # Return a response
    return output
  • 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-05-27T22:17:26+00:00Added an answer on May 27, 2026 at 10:17 pm

    You should probably use exception instead of alarm:

    class AlarmException(Exception): 
        pass 
    
    ...
    
        def alarm_handler(signum, frame):
            raise AlarmException()
    

    Also, do not forget that pipes can overflow. If the process will produce too much (> 64k on some Linuxes) for stderr and/or stdout, program will be blocked.

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

Sidebar

Related Questions

I have been given a requirement where I need to support multiple databases in
I am creating a multilingual site that will need to support at minimum five
The Situation I need to support generating XML documents based on schemas that vary
I'm writing a cross-platform Qt-based program that from time to time needs to play
I just started using jqModal as I need support for nested modals. I'm noticing
Since i'm a poor sql developer, i need support to write a sql query
I need to support exact phrases (enclosed in quotes) in an otherwise space-separated list
I need to support Chinese in one of my application on Windows, the problem
In a current project I need to support finding a User by login credentials
For my Swing project, I need to support both Java 5 and Java 6

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.