[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

is there enough information?

Aaron Brady

2/26/2008 3:30:00 AM

Specify


def thloop( th ):
while th.cont:
with th.step[2]:
th.ret= th.cmd+ 1

def acq( th ):
with th.step[1]:
th.cmd= 100
with th.step[3]:
ret1= th.ret
th.step.reset()
assert ret1== 101

Is it enough?
42 Answers

Dennis Lee Bieber

2/26/2008 5:31:00 AM

0

On Mon, 25 Feb 2008 19:30:08 -0800 (PST), castironpi@gmail.com declaimed
the following in comp.lang.python:

>
> Is it enough?

No...

I see two functions which seem to be based upon accessing some
undefined object but are otherwise totally disjoint...

What are they supposed to be doing?
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Paul McGuire

2/26/2008 5:52:00 AM

0

On Feb 25, 9:30 pm, castiro...@gmail.com wrote:
>
> Is it enough?

(Reminds me of the movie Marathon Man, in which Dustin Hoffman is
repeatedly asked by Laurence Olivier, "Is it safe?" Hoffman had no
idea what Olivier was talking about. So Olivier tortured Hoffman some
more to get the answer.)

Enough for what?

Is "th" short for "thread"? Maybe a little too short - try "thd" at
least, and spelling out "thread" isn't out of the question.

What happened to step[0]? Did you know that lists start at 0? Why
are you using a list of synchronization objects?

You would be better off using attributes of th with meaningful names,
instead of elements of a generically named list attribute, "th.step".
Names like "th.assign_command_synch", "th.return_value_synch", etc.
And since there is no synch object that is locked in both methods,
then the methods will run completely devoid of any synchronization.

Aaron Brady

2/26/2008 7:38:00 AM

0

On Feb 25, 11:52 pm, Paul McGuire <pt...@austin.rr.com> wrote:
> On Feb 25, 9:30 pm, castiro...@gmail.com wrote:
>
>
>
> > Is it enough?
>
> (Reminds me of the movie Marathon Man, in which Dustin Hoffman is
> repeatedly asked by Laurence Olivier, "Is it safe?"  Hoffman had no
> idea what Olivier was talking about.  So Olivier tortured Hoffman some
> more to get the answer.)
>
> Enough for what?
>
> Is "th" short for "thread"?  Maybe a little too short - try "thd" at
> least, and spelling out "thread" isn't out of the question.
>
> What happened to step[0]?  Did you know that lists start at 0?  Why
> are you using a list of synchronization objects?
>
> You would be better off using attributes of th with meaningful names,
> instead of elements of a generically named list attribute, "th.step".
> Names like "th.assign_command_synch", "th.return_value_synch", etc.
> And since there is no synch object that is locked in both methods,
> then the methods will run completely devoid of any synchronization.

I recently ran into a case (* would that be helpful to describe here?)
where thread1 had to do something, thread2 had to do something after
that, and thread1 had to wait for that, then do something else, and
thread2 again had to wait before starting the first thing again.

Clarify:

def thdloop( thd ):
while thd.cont:
thd.sig1event.wait()
ret= thd.cmd()
thd.sig2event.set()
thd.seg3event.wait()

and

def consumer( thd ):


EXCEPT that,

Aaron Brady

2/26/2008 7:45:00 AM

0

> Clarify:
>
> def thdloop( thd ):
>    while thd.cont:
>       thd.sig1event.wait()
>       ret= thd.cmd()
thd.result= ret
>       thd.sig2event.set()
>       thd.seg3event.wait()
>
> and

def consumer( thd ):
thd.cmd= function
thd.sig1event.set()
thd.sig2event.wait()
ret= thd.result
thd.sig3event.set()

Except, I missed the shot on the sigevent.clear() placement, and spent
the better part of the day debugging it.

Two options occurred to me, which the first showed up in the earlier
extremely skeletal and cryptic post: Create a class which will ensure
turn-taking of events, using a get method with and integer index, by
waiting for the prior index to complete before starting the next.

You could name the steps in the class and take a specific order of
them too:

class CmdStep( Step ):
order= SetCommand, CallCommand, ReadResult

so cmdstep= CmdStep() / with cmdstep.SetCommand: or with
cmdstep[SetCommand]: blocks until the specified time.

The other option, was to join both these threads, execute the command,
then split them again: if that's simpler and just as general, then
Step is cool, but rather useless.

Aaron Brady

2/26/2008 1:08:00 PM

0

> Create a class which will ensure
> turn-taking of events, using a get method with and integer index, by
> waiting for the prior index to complete before starting the next.

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 )
lock.release()
def complete( self ):
with self._oplock:
self._index= 0
lock= self._locks.get( 0 )
if None is not lock:
lock.acquire( False )
lock.release()
def open( self ):
with self._oplock:
self._opened= True
for lock in self._locks.values():
lock.acquire( False )
lock.release()

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

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
thd.step.complete()

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__':
func()

@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
case.th1.step.open()
print( 'case complete' )

while 1: time.sleep( 1 )

'''
Kind of trivial, interesting technique, and general. Enter with
thd.step[n]: to indicate step n in a process. with thd.step[m] for m!
= n will block until it exits, and step[n+1] gets the go, or step[0]
with step.complete(). step.open() grants every step, such as in the
case of completion; set termination conditions prior to its call, and
check them in any sensitive loops.

One way to replace magic sequence numbers with meaning is the "Step1
Step2 Step3" style used in namedtuple, but they're only shown bare
here.

Sequence numbers are looked up in a hash; special applications can use
an array, and account for the possibility that with step[10]: may be
called before even step[0]. 0- and 1-based options are customizable.
Sequence length may be known at creation time; if so, the Step
instance can be initialized with it; contraindicating cases may
include with step[n] ... with step[n+2], that don't mean the right
thing in a separate instance.

__getitem__ returns a WithObj, calling __enter__ and __exit__ on which
routes to the Step instance, paired with the specified index. You
could probably cache those.

The example is a 1-SetOp, 2-DoOp, 1-GetReturn triplet.
acquire( False ) / release() pairs release a thread, whether it is
acquired already or not. _oplock synchronizes _index and lock lookup
operations across an instance.

Kind of cool.
'''

Preston Landers

2/26/2008 5:00:00 PM

0

On Feb 26, 1:45 am, castiro...@gmail.com wrote:

> Two options occurred to me, which the first showed up in the earlier
> extremely skeletal and cryptic post:

Perhaps you would be more likely to get the kind of help you seem to
want
if you refrained from posting "cryptic and skeletal" messages. The
fact that many
other people have pointed this out to you as of late would tend to
suggest
you are trolling, i.e. intentionally trying to foster miscommunication
and threads
that do nothing to advance anyones understanding.

And regarding your other recent post about trying to find a "solution"
to the "problem"
of immutable types... Due to the above reasons you are unlikely to
influence the
design of the core language with half-baked stream of consciousness
ramblings. These
belong in your LiveJournal, not in c.l.python.

If you have a problem you need help with, please read this entire
document about 3 times
before posting anything else:

http://catb.org/~esr/faqs/smart-ques...

Specifically this:

http://catb.org/~esr/faqs/smart-ques...#beprecise

and this:

http://catb.org/~esr/faqs/smart-ques...#goal

Aaron Brady

2/26/2008 5:14:00 PM

0

On Feb 26, 10:59 am, Preston Landers <pland...@gmail.com> wrote:
> On Feb 26, 1:45 am, castiro...@gmail.com wrote:
>
> > Two options occurred to me, which the first showed up in the earlier
> > extremely skeletal and cryptic post:
>
> Perhaps you would be more likely to get the kind of help you seem to
> want
> if you refrained from posting "cryptic and skeletal" messages. The
> fact that many
> other people have pointed this out to you as of late would tend to
> suggest
> you are trolling, i.e. intentionally trying to foster miscommunication
> and threads
> that do nothing to advance anyones understanding.
>
> And regarding your other recent post about trying to find a "solution"
> to the "problem"
> of immutable types...  Due to the above reasons you are unlikely to
> influence the
> design of the core language with half-baked stream of consciousness
> ramblings. These
> belong in your LiveJournal, not in c.l.python.
>
> If you have a problem you need help with, please read this entire
> document about 3 times
> before posting anything else:
>
> http://catb.org/~esr/faqs/smart-ques...
>
> Specifically this:
>
> http://catb.org/~esr/faqs/smart-ques...#beprecise
>
> and this:
>
> http://catb.org/~esr/faqs/smart-ques...#goal

Ugh, very well. You call for an explanation.

Back home, the original post would be interesting, so I wrote it.
Whatever reactions other people have to them is information that is
unavailable to me. I don't know you. I'm rather irked by a
proportion of posts, but for my part, it's hard to get me to point a
finger.

I am not a troll. I want a sustainable, healthy, productive,
educational, informative relationship with frequenters of c.l.p, the
Python community at large, and anyone who has anything non-negative to
contribute. If you are wanting to see how I react to hostility, just
ask. I'll fake it for you, but only for a second at a time.

Now, what help is it that you believe I seem to want? All I asked for
was, ideas.

Preston Landers

2/26/2008 5:35:00 PM

0

On Feb 26, 11:13 am, castiro...@gmail.com wrote:

> All I asked for was, ideas.

Nope, you didn't. You said exactly this: "Specify {...} Is it
enough?" and included a snipped of code that was not standalone,
provided no context or explanatory information, and gave us no clue
what you might be trying to accomplish.

To me this does not qualify as asking for ideas. It's more like
posting nonsense with a thin veneer that looks like it's intended to
suggest there is some hidden meaning lurking within and that we are
expected to play 20 questions with you to tease out whatever it is you
might be getting at, presumably some brilliant gem of an insight that
will cause everyone to question our basic intellectual assumptions in
some profound philosophical epiphany.

This has all the hallmarks of being a fun game for you but a waste of
time for us.

Marc 'BlackJack' Rintsch

2/26/2008 5:36:00 PM

0

On Tue, 26 Feb 2008 09:13:35 -0800, castironpi wrote:

> Back home, the original post would be interesting, so I wrote it.

So you think of this group as your personal notepad. That explains a lot. :-/

Ciao,
Marc 'BlackJack' Rintsch

Jeff Schwab

2/26/2008 5:37:00 PM

0

castironpi@gmail.com wrote:
> On Feb 26, 10:59 am, Preston Landers <pland...@gmail.com> wrote:
>> On Feb 26, 1:45 am, castiro...@gmail.com wrote:
>>
>>> Two options occurred to me, which the first showed up in the earlier
>>> extremely skeletal and cryptic post:
>> Perhaps you would be more likely to get the kind of help you seem to
>> want
>> if you refrained from posting "cryptic and skeletal" messages. The
>> fact that many
>> other people have pointed this out to you as of late would tend to
>> suggest
>> you are trolling, i.e. intentionally trying to foster miscommunication
>> and threads
>> that do nothing to advance anyones understanding.
>>
>> And regarding your other recent post about trying to find a "solution"
>> to the "problem"
>> of immutable types... Due to the above reasons you are unlikely to
>> influence the
>> design of the core language with half-baked stream of consciousness
>> ramblings. These
>> belong in your LiveJournal, not in c.l.python.
>>
>> If you have a problem you need help with, please read this entire
>> document about 3 times
>> before posting anything else:
>>
>> http://catb.org/~esr/faqs/smart-ques...
>>
>> Specifically this:
>>
>> http://catb.org/~esr/faqs/smart-ques...#beprecise
>>
>> and this:
>>
>> http://catb.org/~esr/faqs/smart-ques...#goal
>
> Ugh, very well. You call for an explanation.
>
> Back home, the original post would be interesting, so I wrote it.
> Whatever reactions other people have to them is information that is
> unavailable to me. I don't know you. I'm rather irked by a
> proportion of posts, but for my part, it's hard to get me to point a
> finger.
>
> I am not a troll. I want a sustainable, healthy, productive,
> educational, informative relationship with frequenters of c.l.p, the
> Python community at large, and anyone who has anything non-negative to
> contribute. If you are wanting to see how I react to hostility, just
> ask. I'll fake it for you, but only for a second at a time.

Wow. I sure hope I don't come across like castiron does here.

> Now, what help is it that you believe I seem to want? All I asked for
> was, ideas.

It's a little difficult for me to interpret your code, partly because I
am nbt very familiar with Python's support for concurrency. But what
are you trying to a achieve?

You mentioned: "I recently ran into a case (* would that be helpful to
describe here?) where thread1 had to do something, thread2 had to do
something after that, and thread1 had to wait for that, then do
something else, and thread2 again had to wait before starting the first
thing again."

This is ordinarily called a Producer-Consumer model. It is often
implemented using semaphores. Googling "python semaphore" turns up this
documentation:

http://www.python.org/doc/lib/semaphore-ob...

That page, in turn, links to an example of the proper use of semaphores
in Python. Does that help?