is there enough information?

castironpi at castironpi at
Thu Feb 28 09:54:44 CET 2008

On Feb 28, 2:30 am, Dennis Lee Bieber <wlfr... at> wrote:
> "It is time to show your cards or fold"

Here.  Run it.  Download Python 3.0a2.

from thread import start_new_thread as launch
from threading import Lock
import time
from functools import partial

class WithObj:
    def __init__( self, enter, exit ):
        self.__enter__, self.__exit__= enter, exit

class Step:
    def __init__( self ):
        self._index= 0
        self._locks= {}
        self._opened= False
        self._oplock= Lock()
    def __getitem__( self, index ):
        with self._oplock:
            lock= self._locks.get( index, None )
            if None is lock:
                lock= self._locks[ index ]= Lock()
                if index!= self._index:
                    assert lock.acquire( False )
        return WithObj(
            partial( self.ienter, index ),
            partial( self.iexit, index ) )
    def ienter( self, index ):
        with self._oplock:
            if self._opened:
                return self
            lock= self._locks.get( index )
        assert lock.acquire()
        with self._oplock:
            return self
    def iexit( self, index, *a ):
        with self._oplock:
            self._index+= 1
            lock= self._locks.get( self._index )
            if None is not lock:
                lock.acquire( False )
    def complete( self ):
        with self._oplock:
            self._index= 0
            lock= self._locks.get( 0 )
            if None is not lock:
                lock.acquire( False )
    def open( self ):
        with self._oplock:
            self._opened= True
            for lock in self._locks.values():
                lock.acquire( False )

class CustThread:
    count= 0
    def __init__( self ):
        CustThread.count+= 1 CustThread.count
        self.step= Step()
        self.cont= True
        self.cmd= None
        self.ret= None
    def __repr__( self ):
        return '<CustThread %i>'%

def thloop( thd ):
    while thd.cont:
        with thd.step[1]:
            if not thd.cont: break
            print( 'step 1', end= ' ' )
            thd.ret= thd.cmd+ 1
        with thd.step[3]:
            print( 'step 3' )
            thd.ret= None

def op100( thd ):
    with thd.step[0]:
        print( 'step 0', end= ' ' )
        thd.cmd= 100
    with thd.step[2]:
        print( 'step 2', end= ' ' )
        ret1= thd.ret
    assert ret1== 101

def main( func ):
    if __name__== '__main__':

def fmain():
    class Case:
        def __init__( self ):
            self.th1= CustThread()

    while 1:
        print( '===============================' )
        class Case1:
            case= Case()
            launch( thloop, ( case.th1, ) )
            for _ in range( 10 ):
                case.th1.cmd= None
                case.th1.ret= None
                op100( case.th1 )
            case.th1.cont= False
        print( 'case complete' )

    while 1: time.sleep( 1 )

What results do you get on your computer?

More information about the Python-list mailing list