[PEAK] Re: PEAK design pointers

Paul Moore pf_moore at yahoo.co.uk
Sun Mar 7 10:29:34 EST 2004


"Phillip J. Eby" <pje at telecommunity.com> writes:

>> > Well, if I understand you correctly, I'd probably write something that
>> > would spawn an events.Task for each of the databases to be monitored,
>> > and create another Task that accepted the results of each of the other
>> > tasks in order to prepare the consolidated report and mail it out.
>>
>>Right - it sounds like events.Task is the place for me to look. That's
>>excellent. As I said, it's pointers to the right places to look which
>>I was lacking...
>
> Note that normally the way you'll do tasks is to wrap an object method
> with events.taskFactory.  E.g.:
>
>      def someMethod(self,...):
>          # blah blah
>          yield something; events.resume()
>          # ...
>
>      someMethod = events.taskFactory(someMethod)
>
> Then, calling 'someMethod(...)' will return a running 'events.Task'
> for that invocation of the method.

Hmm, I've read the events module, and as far as I can see it's aimed
at a cooperative multitasking model. That doesn't seem to fit my
application structure at all.

On the other hand, your suggestion of using Twisted's deferToThread
does help a lot. (I never thought of using Twisted for a non-network
application...)

I'll give you some detail of the simplest of my applications. Maybe
it will make things clearer. I won't try to include PEAK style
configuration or binding, as I think it'd only obscure the issue at
the moment. (And I think I know how to handle that)

Assume I have a generator function DBs(), which generates a series of
cx_Oracle connect strings, one after the other. My trivial application
just wants to try to connect to each, and report all the failures.

errors = 0

def recordFailure(f):
    f.trap(cx_Oracle.DatabaseError)
    global errors
    errors += 1

for db in dbs:
    d = threads.deferToThread(cx_Oracle.connect, db)
    d.addErrback(recordFailure)

# Really, stop when all threads have run
reactor.callLater(10, reactor.stop)
reactor.run()

# Placeholder. Really send an email
print errors
    
The more complex applications will use a callback on the deferred to
save some query results in the repository.

I'm not sure I see a need for PEAK tasks here. Then again, using
twisted's reactor simplified the whole application so much that maybe
I don't need anything that complex now!

Thanks,
Paul
-- 
This signature intentionally left blank




More information about the PEAK mailing list