Ejemplo n.º 1
0
    def testSetGet2(self):
        """
        Swap data between three threads
        """
        r1 = WriteOnceVar()
        r2 = WriteOnceVar()
        x = WriteOnceVar()
        y = WriteOnceVar()

        def t1():
            x.set("one")
            r1.set(y.get())

        def t2():
            y.set("two")
            r2.set(x.get())

        th1 = th.Thread(target=t1)
        th1.start()

        self.assertEquals(x.get(), "one")
        self.assertTrue(x.isSet())

        th2 = th.Thread(target=t2)
        th2.start()

        th1.join()
        th2.join()
        self.assertEquals(r1.get(), "two")
        self.assertEquals(r2.get(), "one")
Ejemplo n.º 2
0
    def testReply(self):
        '''
        Send reply ivar down request ivar 
        '''
        requestCh = WriteOnceVar()

        def t():
            reply = requestCh.get()
            reply.set("hello!!")

        th.Thread(target=t).start()
        replyCh = WriteOnceVar()
        requestCh.set(replyCh)
        self.assertEquals(replyCh.get(), "hello!!")
Ejemplo n.º 3
0
    def testRecurse3(self):
        """
        See if recursion fails for large number of processes.
        Have this as last test as it ties up lots of threads
        """
        res = rec_fib(15)
        from PyDFlow.writeonce import WriteOnceVar
        resslot = WriteOnceVar()

        def waiter(resslot):
            print "waiter running"
            f = res.get()
            print "waiter got %d" % f
            resslot.set(f)
            print "waiter done %s" % repr(resslot)

        t = th.Thread(target=waiter, args=(resslot, ))
        t.start()

        # 10 seconds
        print "waiting for fibonacci result"
        for i in range(10):
            #while True:
            print resslot
            if resslot.isSet():
                self.assertEquals(resslot.get(), 610)
                return
            print ".",
            time.sleep(1)
        print(resslot.get())
        self.fail("Ran out of time waiting for recursive fibonacci calc")
Ejemplo n.º 4
0
 def testSetGet(self):
     x = WriteOnceVar()
     setTh = th.Thread(target=(lambda: x.set(32)))
     setTh.run()
     self.assertEquals(x.get(), 32)
     self.assertEquals(x.get(), 32)
     self.assertEquals(x.get(), 32)
     self.assertEquals(x.get(), 32)
Ejemplo n.º 5
0
    def __init__(self, *args, **kwargs):
        super(AtomicIvar, self).__init__(*args, **kwargs)

        # Create the future
        self._future = WriteOnceVar()
        # __future stores a handle to the underlying data
        # future will be set exactly when the underlying data is
        # ready for reading: this way the get() function can block
        # on the future

        # Whether the backing storage is reliable
        self._reliable = True
Ejemplo n.º 6
0
def init():
    global in_queue, resume_queue, workers, work_deques
    logging.debug("Initialising thread pool")
    in_queue = Queue.Queue()
    resume_queue = Queue.Queue()
    #TODO: this may not be a good memory layout, likely
    # to cause cache conflict
    work_deques = [deque() for i in range(NUM_THREADS)]
    for i in range(NUM_THREADS):
        t = WorkerThread(in_queue, resume_queue, i, work_deques[i])
        workers.append(t)
        t.start()


# Use future to ensure init() run exactly once
initFuture = WriteOnceVar(function=init)


def exec_async(ivar):
    """
    Takes a ivar and fills it at some point in the future
    """
    # Ensure workers are initialized
    logging.debug("Entered exec_async")
    initFuture.get()
    logging.debug("Added ivar to work queue")
    with idle_worker_cvar:
        in_queue.put(ivar)
        idle_worker_cvar.notifyAll()
    logging.debug("Exiting exec_async")
Ejemplo n.º 7
0
 def __init__(self, expected_class):
     Placeholder.__init__(self, expected_class)
     Ivar.__init__(self) # Inputs and outputs will be managed
     self._proxy_for = WriteOnceVar()
Ejemplo n.º 8
0
 def testIsSet(self):
     x = WriteOnceVar()
     self.assertFalse(x.isSet())
     x.set("hello")
     self.assertTrue(x.isSet())
Ejemplo n.º 9
0
 def testGet(self):
     x = WriteOnceVar()
     x.set(32)
     self.assertEquals(x.get(), 32)