timeout_advice.py :  » Aspect-Oriented » aspects.py » python-aspects-1.3 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Aspect Oriented » aspects.py 
aspects.py » python aspects 1.3 » examples » timeout_advice.py
from thread import start_new_thread,allocate_lock
import time

class _Thread_return_stop_holder: pass

def timeout_2s_catch(self, *args, **keyw):
    """
    This function implements 2 second timeout advice
    that catches exceptions raised by the thread.
    The advice equals to the one you get by
    a = create_timeout_advice( 2.0, 1 )
    """

    # Initialise tflock that is locked until thread
    # finishes execution
    tflock = allocate_lock()
    tflock.acquire()
    
    ret = _Thread_return_stop_holder()
    ret.value = None
    
    def save_retval( ret ):
        try: ret.value = self.__proceed(*args, **keyw)
        except: pass
        tflock.release()

    start_new_thread( save_retval, (ret,) )
    
    if _wait_timeout( 2.0, tflock ):
        return None

    return ret.value


_num_of_advices = 0

def create_timeout_advice( timeout = 1.0, catch_exceptions = 1 ):
    global _num_of_advices
    _num_of_advices = _num_of_advices + 1
    py = "def timeout_advice" + str( _num_of_advices ) + \
         "(self, *args, **keyw):\n" + \
         "    tflock = allocate_lock()\n" + \
         "    tflock.acquire()\n" + \
         "    ret = _Thread_return_stop_holder()\n" + \
         "    ret.value = None\n" + \
         "    def save_retval( ret ):\n"
    if catch_exceptions:
        py = py + "        try: ret.value = self.__proceed(*args,**keyw)\n" + \
                  "        except: pass\n"
    else:
        py = py + "        ret.value = self.__proceed(*args,**keyw)\n"
    py = py + "        tflock.release()\n" + \
         "    start_new_thread( save_retval, (ret,) )\n" + \
         "    if _wait_timeout( "+str(timeout)+", tflock):\n" + \
         "        return None\n" + \
         "    return ret.value\n"
    return _create_function( py, "timeout_advice"+str(_num_of_advices) )


def _create_function( function_code, function_name ):
    # compile code
    codeobj = compile( function_code, "", "exec" )
    # execute code, new function is added to local name space
    exec( codeobj )
    return eval( function_name )

def _wait_timeout( max_time, lock ):
    """
    Return 1 if lock is not released before max_time
    seconds have elapsed.
    """
    start_time = time.time()
    while time.time() - start_time < max_time:
        time.sleep( 0.01 )
        if lock.acquire(0):
            return 0
    return 1
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.