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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:39:20+00:00 2026-05-27T04:39:20+00:00

Possible Duplicate: Timeout on a Python function call I want to implement that when

  • 0

Possible Duplicate:
Timeout on a Python function call

I want to implement that when the function took more than 90 seconds to complete it should return immediately when timeout. Is there any way to achieve that?

def abc(string):
    import re
    if re.match('some_pattern', string):
        return True
    else:
        return False

abc('some string to match')

Edited

Please download this test file. I have created a thread class and raise an exception within thread if timeout error occur. But thread is still alive because it prints i am still alive :) even after exception. Why an exception does not force the thread to stop??

  • 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-27T04:39:20+00:00Added an answer on May 27, 2026 at 4:39 am

    I’ve edited my post to use jcollado’s idea which is simpler.

    The multiprocessing.Process.join method has a timeout argument which you can use like this:

    import multiprocessing as mp
    import time
    import logging  
    import re
    
    logger = logging.getLogger(__name__)
    
    def abc(string, result, wait = 0):
        time.sleep(wait)
        result.put(bool(re.match('some_pattern', string)))
    
    if __name__ == '__main__':
        logging.basicConfig(level = logging.DEBUG,
                            format = '%(asctime)s:  %(message)s',
                            datefmt = '%H:%M:%S', )
        result = mp.Queue()
        proc = mp.Process(target = abc, args = ('some_pattern to match', result))
        proc.start()
        proc.join(timeout = 5)
        if proc.is_alive():
            proc.terminate()
        else:
            logger.info(result.get())
    
        proc = mp.Process(target = abc, args = ('some string to match', result, 20))
        proc.start()
        proc.join(timeout = 5)
        if proc.is_alive():
            logger.info('Timed out')
            proc.terminate()
        else:
            logger.info(result.get())
    

    yields

    12:07:59:  True
    12:08:04:  Timed out
    

    Note that you get the “Timed out” message in 5 seconds, even though abc('some string',20) would have taken around 20 seconds to complete.

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

Sidebar

Related Questions

Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Unload a module in Python After importing Numpy, lets say I want
Possible Duplicate: Reset JavaScript Counter? I'm trying to create a function that will trigger
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: setTimeout and “this” in JavaScript I am trying to put a timeout

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.