Thomas P Jakobsen <thomas....@gmail.com> writes:

> By the way, the same thing seem sto happen if the protocol, insted of
> asking the user to press enter, does some local computations like
> quering a database, sleeping, computing primes or the like. [...]

Right -- callback functions *cannot block*! They must always returns
very quickly and use some asynchronous mechanism to signal when a
long-running operations is finished. See this FAQ:

  
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoIuseDeferredstomakemyblockingcodenon-blocking

Twisted has a database abstraction layer that does this for databases:

  http://twistedmatrix.com/projects/core/documentation/howto/rdbms.html

And a threadpool for more general tasks:

  http://twistedmatrix.com/projects/core/documentation/howto/threading.html

For non-blocking access to stdin and stdout there is the
twisted.internet.stdio module, see:

  http://twistedmatrix.com/trac/browser/trunk/doc/core/examples/stdiodemo.py

This means that your example should look similar to this:

#!/usr/bin/python

import sys
From optparse import OptionParser

From twisted.internet import reactor, stdio, defer
From twisted.protocols import basic

From viff.field import GF
From viff.runtime import create_runtime, gather_shares, Share
From viff.comparison import Toft05Runtime
From viff.config import load_config

class SomeProtocol(object):

    def __init__(self, runtime, io):
        self.runtime = runtime

        # First do some local stuff.
        io.sendLine("Press Enter!")
        line = io.readLine()
        line.addCallback(self.go)

    def go(self, line):
        # Now use VIFF.
        Zp = GF(30916444023318367583)
        a = Share(self.runtime, Zp, value=Zp(0))
        b = Share(self.runtime, Zp, value=Zp(1))
        c = a > b
        c.addCallback(lambda _: self.runtime.shutdown())

class WaitForLine(basic.LineReceiver):
    from os import linesep as delimiter
    deferred = None

    def lineReceived(self, line):
        self.sendLine("* received: %s" % line)
        if self.deferred:
            d, self.deferred = self.deferred, None
            d.callback(line)
        else:
            self.sendLine("** no deferred available!")

    def readLine(self):
        self.deferred = defer.Deferred()
        return self.deferred

if __name__ == "__main__":
    io = WaitForLine()
    stdio.StandardIO(io)

    # Set up VIFF.
    parser = OptionParser()
    Toft05Runtime.add_options(parser)
    options, args = parser.parse_args()
    id, players = load_config(args[0])
    pre_runtime = create_runtime(id, players, 1, options, Toft05Runtime)
    pre_runtime.addCallback(SomeProtocol, io)
    reactor.run()
-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.

Attachment: pgpcdjr8YHEdg.pgp
Description: PGP signature

_______________________________________________
viff-devel mailing list (http://viff.dk/)
viff-devel@viff.dk
http://lists.viff.dk/listinfo.cgi/viff-devel-viff.dk

Reply via email to