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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:23:26+00:00 2026-05-11T06:23:26+00:00

I’m trying to write a TTL decorator in python. Basically I give it raise

  • 0

I’m trying to write a TTL decorator in python. Basically I give it raise an exception if the function doesn’t answer in the selected time.

You can find the thead2 snippets on http://sebulba.wikispaces.com/recipe+thread2

from thread2 import Thread  '''  A TTL decorator. ''' class Worker(Thread):     def __init__(self, q, f, args, kvargs):         Thread.__init__(self)          self.q = q         self.f = f         self.args = args         self.kvargs = kvargs      def run(self,):         try:             res = (True, self.f(*self.args, **self.kvargs))             self.q.put(res)         except Exception, e:             self.q.put((False, e))  class Referee(Thread):     def __init__(self,q, ttl,exception_factory):         Thread.__init__(self)          self.exception_factory=exception_factory             self.q=q         self.ttl=ttl      def run(self):         time.sleep(self.ttl)         res = (False, self.exception_factory())         self.q.put(res)  def raise_if_too_long(ttl, exception_factory=lambda :RuntimeError('Timeout')):     def raise_if_too_long_aux(f):         def ritl(*args,**kvargs):             q = Queue.Queue(2)              referee = Referee(q, ttl, exception_factory)             worker = Worker(q,f,args,kvargs)              worker.start()             referee.start()              (valid, res)= q.get(1)              q.task_done()              referee.terminate()             worker.terminate()              if valid:                 return res             else:                 raise res          return ritl      return raise_if_too_long_aux 

However, I get some pretty bad result. It seems like sometimes the function is returning alright yet the decorator doesn’t return until the TTL is reached and the error raises.

Do you see something wrong in this code? Is there a common way/library to write function with a TTL in python?

  • 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. 2026-05-11T06:23:27+00:00Added an answer on May 11, 2026 at 6:23 am

    The code provided is a bit tough to follow — how is it going to raise an exception in the right place at the right time in the right thread?

    Consider this rough flow:

    Decorator function called with target function. Return a function which:

    1. Starts thread, calling target function
    2. Joins to thread using thread.join([timeout])
    3. If you get a timeout, raise an exception, and ignore the result of the thread.
    4. If you don’t get a timeout, capture the result of the thread and return it.

    (You would need to devise a way to capture the output of the thread…)

    See http://docs.python.org/library/threading.html for info on the threading timeout…

    (Or just start using erlang :)

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

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The win32/ie package can be found in the JExplorer librairie… May 12, 2026 at 11:35 am
  • Editorial Team
    Editorial Team added an answer We're just using the standard View Based application template for… May 12, 2026 at 11:35 am
  • Editorial Team
    Editorial Team added an answer as David Andres mentions in the comment, DATE() extracts the… May 12, 2026 at 11:35 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.