예제 #1
0
    def testChannel(self):

        c = stackless.channel()

        recvd = []

        def ch1():
            for i in range(10):
                recvd.append(c.receive())

        def ch2():
            for i in range(10):
                c.send(i)

        child1 = stackless.tasklet(ch1)()
        child2 = stackless.tasklet(ch2)()

        self.assertEquals(3, stackless.getruncount())  #main

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals(range(10), recvd)

        self.assertEquals(1, stackless.getruncount())  #main
예제 #2
0
def a_main_tasklet():
    global running

    threadID = GetThreadID()

    for i in range(3):
        stackless.tasklet(looping_tasklet)(threadID, i + 1)

    # We need to catch the keyboard interrupt and signal the other thread to exit.
    try:
        print threadID, "start: runcount", stackless.getruncount()

        # Do a preliminary run to get some tasklets in the scheduler or some
        # sleeping tasklets in place, otherwise we would not run at all.
        stackless.run()

        # Now we should be set to run until we are done.
        while running and (stackless.getruncount() > 1
                           or sleepCountByThread.get(threadID, 0)):
            print threadID, "start: runcount", stackless.getruncount(
            ), "sleepcount", sleepCountByThread.get(threadID, 0)
            stackless.run()
        print threadID, "stop: runcount", stackless.getruncount(
        ), "sleepcount", sleepCountByThread.get(threadID, 0)
    except:
        running = False
        raise
예제 #3
0
    def testChannel(self):

        c = stackless.channel()

        recvd = []

        def ch1():
            for i in range(10):
                recvd.append(c.receive())

        def ch2():
            for i in range(10):
                c.send(i)

        child1 = stackless.tasklet(ch1)()
        child2 = stackless.tasklet(ch2)()

        self.assertEquals(3, stackless.getruncount()) #main

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals(range(10), recvd)

        self.assertEquals(1, stackless.getruncount()) #main
    def testRuncount(self):
        """Test stackless.runcount. It is a per thread value"""
        rc1 = stackless.runcount
        rc2 = stackless.getruncount()
        self.assertIsInstance(rc1, int)
        self.assertIsInstance(rc2, int)
        self.assertEqual(rc1, rc2)
        self.assertGreaterEqual(rc1, 1)

        c = []

        def f():
            c.append(stackless.runcount)
            c.append(stackless.getruncount())

        tlet = stackless.tasklet(f)()

        # runcount is now at least 2
        self.assertEqual(stackless.runcount, rc1 + 1)
        self.assertEqual(stackless.getruncount(), rc1 + 1)

        t = threading.Thread(target=f)
        t.start()
        t.join()

        self.assertListEqual(c, [1, 1])

        tlet.run()

        self.assertListEqual(c, [1, 1, rc1 + 1, rc1 + 1])
        self.assertEqual(stackless.runcount, rc1)
        self.assertEqual(stackless.getruncount(), rc1)
예제 #5
0
    def testSchedule(self):

        res1 = []
        res2 = []

        def ch1():
            for i in range(10):
                res1.append((i, stackless.getruncount()))
                stackless.schedule()

        def ch2():
            for i in range(10):
                res2.append((i, stackless.getruncount()))
                stackless.schedule()

        child1 = stackless.tasklet(ch1)()
        child2 = stackless.tasklet(ch2)()

        self.assertEquals(3, stackless.getruncount())  #main + ch1, ch2

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3),
                           (6, 3), (7, 3), (8, 3), (9, 3)], res1)
        self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3),
                           (6, 3), (7, 3), (8, 3), (9, 3)], res2)

        self.assertEquals(1, stackless.getruncount())  #main
예제 #6
0
    def testSchedule(self):

        res1 = []
        res2 = []

        def ch1():
            for i in range(10):
                res1.append((i, stackless.getruncount()))
                stackless.schedule()

        def ch2():
            for i in range(10):
                res2.append((i, stackless.getruncount()))
                stackless.schedule()

        child1 = stackless.tasklet(ch1)()
        child2 = stackless.tasklet(ch2)()

        self.assertEquals(3, stackless.getruncount()) #main + ch1, ch2

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res1)
        self.assertEquals([(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3)], res2)

        self.assertEquals(1, stackless.getruncount()) #main
예제 #7
0
 def doit():
     result = []
     child1 = stackless.tasklet(ch1)(result)
     child2 = stackless.tasklet(ch2)(result)
     self.assertEquals(3, stackless.getruncount())
     while stackless.getruncount() > 1:
         stackless.schedule()
     return result
예제 #8
0
 def doit():
     result = []
     child1 = stackless.tasklet(ch1)(result)
     child2 = stackless.tasklet(ch2)(result)
     self.assertEquals(3, stackless.getruncount())
     while stackless.getruncount() > 1:
         stackless.schedule()
     return result
예제 #9
0
def recv_tasklet():
    while True:
        print "recv enter %d" % stackless.getruncount()
#        print "recv data: " + channel.receive()
        try:
            print "recv data: " + channel.receive()
        except Exception,e:
            print "recv get cexeption:" + e.message
        print "recv leave %d" % stackless.getruncount()
예제 #10
0
 def setUp(self):
     self.assertEqual(
         stackless.getruncount(), 1,
         "Leakage from other tests, with %d tasklets still in the scheduler"
         % (stackless.getruncount() - 1))
     if withThreads:
         self.assertEqual(
             threading.activeCount(), 1,
             "Leakage from other threads, with %d threads running (1 expected)"
             % (threading.activeCount()))
예제 #11
0
 def setUp(self):
     self.assertEqual(
         stackless.getruncount(), 1,
         "Leakage from other tests, with %d tasklets still in the scheduler"
         % (stackless.getruncount() - 1))
     expected_thread_count = self.setUpStacklessTestCase()
     if withThreads:
         active_count = threading.active_count()
         self.assertEqual(
             active_count, expected_thread_count,
             "Leakage from other threads, with %d threads running (%d expected)"
             % (active_count, expected_thread_count))
예제 #12
0
    def test_getruncount(self):
        assert stackless.getruncount() == 1
        def with_schedule():
            assert stackless.getruncount() == 2

        t1 = stackless.tasklet(with_schedule)()
        assert stackless.getruncount() == 2
        stackless.schedule()
        def with_run():
            assert stackless.getruncount() == 1

        t2 = stackless.tasklet(with_run)()
        stackless.run()
예제 #13
0
    def test_getruncount(self):
        assert stackless.getruncount() == 1
        def with_schedule():
            assert stackless.getruncount() == 2

        t1 = stackless.tasklet(with_schedule)()
        assert stackless.getruncount() == 2
        stackless.schedule()
        def with_run():
            assert stackless.getruncount() == 1

        t2 = stackless.tasklet(with_run)()
        stackless.run()
예제 #14
0
def runThread(func):
    #lock.acquire()
    print(stackless.getruncount(), stackless.getthreads())
    numCurrentThreads = stackless.getthreads()
    latestThread = numCurrentThreads[-1]
    numCurrentThreads = len(numCurrentThreads)
    subthreads = list()
    for i in range(numCurrentThreads):
        subthreads.append(Thread(target=checkedRun, args=(func, )))
        subthreads[-1].start()
    print(stackless.getruncount(), stackless.getthreads())
    print(stackless.main)
    time.sleep(2)
예제 #15
0
    def setUp(self):
        self._ran_AsTaskletTestCase_setUp = True
        if stackless.enable_softswitch(None):
            self.assertEqual(stackless.current.nesting_level, 0)

        # yes, its intended: call setUp on the grand parent class
        super(StacklessTestCase, self).setUp()
        expected_thread_count = self.setUpStacklessTestCase()
        self.assertEqual(stackless.getruncount(
        ), 1, "Leakage from other tests, with %d tasklets still in the scheduler" % (stackless.getruncount() - 1))
        if withThreads:
            active_count = threading.active_count()
            self.assertEqual(active_count, expected_thread_count, "Leakage from other threads, with %d threads running (%d expected)" % (active_count, expected_thread_count))
예제 #16
0
 def child(r):
     r.b1 = c.balance
     r.rc1 = stackless.getruncount()
     r.cur1 = stackless.getcurrent()
     r.blocked1 = r.cur1.blocked
     r.alive1 = r.cur1.alive
     try:
         c.receive()
     finally:
         r.b2 = c.balance
         r.rc2 = stackless.getruncount()
         r.cur2 = stackless.getcurrent()
         r.blocked2 = r.cur2.blocked
         r.alive2 = r.cur2.alive
예제 #17
0
 def child(r):
     r.b1 = c.balance
     r.rc1 = stackless.getruncount()
     r.cur1 = stackless.getcurrent()
     r.blocked1 = r.cur1.blocked
     r.alive1 = r.cur1.alive
     try:
         c.receive()
     finally:
         r.b2 = c.balance
         r.rc2 = stackless.getruncount()
         r.cur2 = stackless.getcurrent()
         r.blocked2 = r.cur2.blocked
         r.alive2 = r.cur2.alive
예제 #18
0
 def test_insert_balance(self):
     """ Test that insert into the runqueue of a remote thread does not affect the
     bookkeeping of the current thread.
     """
     thread, task = self.create_thread_task()
     try:
         task.remove()
         before = stackless.getruncount()
         task.insert()
         after = stackless.getruncount()
         # only the runnable count on the remote thread
         # should change
         self.assertEqual(before, after)
     finally:
         thread.join()
예제 #19
0
 def test_insert_balance(self):
     """ Test that insert into the runqueue of a remote thread does not affect the
     bookkeeping of the current thread.
     """
     thread, task = self.create_thread_task()
     try:
         task.remove()
         before = stackless.getruncount()
         task.insert()
         after = stackless.getruncount()
         # only the runnable count on the remote thread
         # should change
         self.assertEqual(before, after)
     finally:
         thread.join()
예제 #20
0
    def run(channel, func, *args, **kwargs):
        r"""
        Runs function `func` with the given arguments on jobman
        channel `chan`.
        """
        try:
            with open('state.ckpt', 'rb') as f:
                t = load(f)
            assert t.restorable
            t.insert()
        except IOError:
            t = stackless.tasklet(func)(*args, **kwargs)

        while stackless.getruncount() > 1:
            stackless.schedule()
            mess = channel.switch()
            if mess is not None:
                with open('state.tmp', 'wb') as f:
                    save(t, f)
                os.rename('state.tmp', 'state.ckpt')
                channel.save()
                if mess == 'stop' or mess == 'finish-up':
                    t.kill()
                    return channel.INCOMPLETE
        channel.save()
        return channel.COMPLETE
예제 #21
0
    def run(channel, func, *args, **kwargs):
        r"""
        Runs function `func` with the given arguments on jobman
        channel `chan`.
        """
        try:
            with open('state.ckpt', 'rb') as f:
                t = load(f)
            assert t.restorable
            t.insert()
        except IOError:
            t = stackless.tasklet(func)(*args, **kwargs)

        while stackless.getruncount() > 1:
            stackless.schedule()
            mess = channel.switch()
            if mess is not None:
                with open('state.tmp', 'wb') as f:
                    save(t, f)
                os.rename('state.tmp', 'state.ckpt')
                channel.save()
                if mess == 'stop' or mess == 'finish-up':
                    t.kill()
                    return channel.INCOMPLETE
        channel.save()
        return channel.COMPLETE
예제 #22
0
def ManageSleepingTasklets(threadID):
    global sleepingTasklets, lock, running, threadIDByChannelID, sleepCountByThread

    sleepingTasklets = []
    while running:
        if len(sleepingTasklets):
            lock.acquire(True)
            endTime = sleepingTasklets[0][0]
            if endTime <= time.time():
                channel = sleepingTasklets[0][1]
                del sleepingTasklets[0]
                threadID = threadIDByChannelID[id(channel)]
                sleepCountByThread[threadID] -= 1
                lock.release()

                # We have to send something, but it doesn't matter what as it is not used.
                channel.send(None)
            else:
                lock.release()
        elif stackless.getruncount() == 1:
            # Give up if there are no more sleeping tasklets.  Otherwise the two
            # threads keep on running endlessly.
            print "Sleeping tasklet exited due to no remaining work."
            break
        stackless.schedule()
    else:
        print threadID, "Sleeping tasklet exited due to change in 'running' flag"
def ManageSleepingTasklets(threadID):
    global sleepingTasklets, lock, running, threadIDByChannelID, sleepCountByThread

    sleepingTasklets = []
    while running:
        if len(sleepingTasklets):
            lock.acquire(True)
            endTime = sleepingTasklets[0][0]
            if endTime <= time.time():
                channel = sleepingTasklets[0][1]
                del sleepingTasklets[0]
                threadID = threadIDByChannelID[id(channel)]
                sleepCountByThread[threadID] -= 1
                lock.release()

                # We have to send something, but it doesn't matter what as it is not used.
                channel.send(None)
            else:
                lock.release()
        elif stackless.getruncount() == 1:
            # Give up if there are no more sleeping tasklets.  Otherwise the two
            # threads keep on running endlessly.
            print "Sleeping tasklet exited due to no remaining work."
            break
        stackless.schedule()
    else:
        print threadID, "Sleeping tasklet exited due to change in 'running' flag"
예제 #24
0
  def testSendInsert(self):
    channel_obj = stackless.channel()
    self.assertEqual(None, channel_obj.queue)
    tasklet1 = stackless.tasklet(lambda: 1 / 0)()
    tasklet2 = stackless.tasklet(channel_obj.receive)()
    tasklet2.run()
    self.assertRaisesStr(
        RuntimeError, 'You cannot remove a blocked tasklet.',
        tasklet2.remove)
    # channel_obj.send inserts tasklet2 after current, and since tasklet1 was
    # after current, the insertion runs tasklet1 eventually, which triggers
    # the ZeroDivisionError, propagated to current (== main).
    self.assertRaises(ZeroDivisionError, channel_obj.send, 0)
    self.assertEqual(1, stackless.getruncount())
    self.assertEqual(None, channel_obj.queue)

    channel_obj.preference = 1  # Prefer the sender.
    tasklet1 = stackless.tasklet(lambda: 1 / 0)()
    tasklet2 = stackless.tasklet(channel_obj.receive)()
    self.assertEqual(False, tasklet2.blocked)
    self.assertEqual(True, tasklet2.scheduled)
    tasklet2.run()
    self.assertEqual(True, tasklet2.blocked)
    self.assertEqual(True, tasklet2.scheduled)
    self.assertEqual(tasklet1, stackless.getcurrent().next)
    self.assertEqual(None, channel_obj.send(0))
    self.assertEqual(tasklet1, stackless.getcurrent().next)
    self.assertEqual(tasklet2, stackless.current.prev)
    tasklet1.remove()
    stackless.schedule()
예제 #25
0
def second2():
    print("Function Second2")
    while True:
        time.sleep(1)
        #stackless.tasklet(first)()
        stackless.schedule()
        print(stackless.getruncount())
예제 #26
0
    def testInterthreadCommunication(self):
        ''' Test that tasklets in different threads sending over channels to each other work. '''
        self.assertEqual(
            stackless.getruncount(), 1,
            "Leakage from other tests, with tasklets still in the scheduler.")

        commandChannel = stackless.channel()

        def master_func():
            commandChannel.send("ECHO 1")
            commandChannel.send("ECHO 2")
            commandChannel.send("ECHO 3")
            commandChannel.send("QUIT")

        def slave_func():
            while 1:
                command = commandChannel.receive()
                if command == "QUIT":
                    break

        def scheduler_run(tasklet_func):
            t = stackless.tasklet(tasklet_func)()
            while t.alive:
                stackless.run()

        thread = threading.Thread(target=scheduler_run, args=(master_func, ))
        thread.start()

        scheduler_run(slave_func)
예제 #27
0
파일: test_channel.py 프로젝트: d11/rts
    def testInterthreadCommunication(self):
        ''' Test that tasklets in different threads sending over channels to each other work. '''    
        self.assertEqual(stackless.getruncount(), 1, "Leakage from other tests, with tasklets still in the scheduler.")

        commandChannel = stackless.channel()

        def master_func():
            commandChannel.send("ECHO 1")
            commandChannel.send("ECHO 2")
            commandChannel.send("ECHO 3")
            commandChannel.send("QUIT")

        def slave_func():
            while 1:
                command = commandChannel.receive()
                if command == "QUIT":
                    break

        def scheduler_run(tasklet_func):
            t = stackless.tasklet(tasklet_func)()
            while t.alive:
                stackless.run()

        thread = threading.Thread(target=scheduler_run, args=(master_func,))
        thread.start()

        scheduler_run(slave_func)
예제 #28
0
    def testSendInsert(self):
        channel_obj = stackless.channel()
        self.assertEqual(None, channel_obj.queue)
        tasklet1 = stackless.tasklet(lambda: 1 / 0)()
        tasklet2 = stackless.tasklet(channel_obj.receive)()
        tasklet2.run()
        self.assertRaisesStr(RuntimeError,
                             'You cannot remove a blocked tasklet.',
                             tasklet2.remove)
        # channel_obj.send inserts tasklet2 after current, and since tasklet1 was
        # after current, the insertion runs tasklet1 eventually, which triggers
        # the ZeroDivisionError, propagated to current (== main).
        self.assertRaises(ZeroDivisionError, channel_obj.send, 0)
        self.assertEqual(1, stackless.getruncount())
        self.assertEqual(None, channel_obj.queue)

        channel_obj.preference = 1  # Prefer the sender.
        tasklet1 = stackless.tasklet(lambda: 1 / 0)()
        tasklet2 = stackless.tasklet(channel_obj.receive)()
        self.assertEqual(False, tasklet2.blocked)
        self.assertEqual(True, tasklet2.scheduled)
        tasklet2.run()
        self.assertEqual(True, tasklet2.blocked)
        self.assertEqual(True, tasklet2.scheduled)
        self.assertEqual(tasklet1, stackless.getcurrent().next)
        self.assertEqual(None, channel_obj.send(0))
        self.assertEqual(tasklet1, stackless.getcurrent().next)
        self.assertEqual(tasklet2, stackless.current.prev)
        tasklet1.remove()
        stackless.schedule()
예제 #29
0
 def tearDown(self):
     # Tasklets created in pickling tests can be left in the scheduler when they finish.  We can feel free to
     # clean them up for the tests.  Any tests that expect to exit with no leaked tasklets should do explicit
     # assertions to check.
     self.assertTrue(self.__setup_called, "Broken test case: it didn't call super(..., self).setUp()")
     self.__setup_called = False
     mainTasklet = stackless.getmain()
     current = mainTasklet.next
     while current is not None and current is not mainTasklet:
         next_ = current.next
         current.kill()
         current = next_
     run_count = stackless.getruncount()
     self.assertEqual(run_count, 1, "Leakage from this test, with %d tasklets still in the scheduler" % (run_count - 1))
     if withThreads:
         preexisting_threads = self.__preexisting_threads
         self.__preexisting_threads = None  # avoid pickling problems, see _addSkip
         expected_thread_count = len(preexisting_threads)
         active_count = threading.active_count()
         if active_count > expected_thread_count:
             activeThreads = set(threading.enumerate())
             activeThreads -= preexisting_threads
             self.assertNotIn(threading.current_thread(), activeThreads, "tearDown runs on the wrong thread.")
             while activeThreads:
                 activeThreads.pop().join(0.5)
             active_count = threading.active_count()
         self.assertEqual(active_count, expected_thread_count, "Leakage from other threads, with %d threads running (%d expected)" % (active_count, expected_thread_count))
     gc.collect()  # emits warnings about uncollectable objects after each test
예제 #30
0
 def tearDown(self):
   self.assertEqual(stackless.main, stackless.getcurrent())
   main_tasklet = stackless.main
   try:
     self.assertEqual(1, stackless.getruncount())
   finally:
     while main_tasklet is not main_tasklet.prev:
       main_tasklet.prev.kill()
예제 #31
0
    def testMainTaskletBlockingWithoutASender(self):
        ''' Test that the last runnable tasklet cannot be blocked on a channel. '''
        self.assertEqual(
            stackless.getruncount(), 1,
            "Leakage from other tests, with tasklets still in the scheduler.")

        c = stackless.channel()
        self.assertRaises(RuntimeError, c.receive)
예제 #32
0
 def tearDown(self):
     self.assertEqual(stackless.main, stackless.getcurrent())
     main_tasklet = stackless.main
     try:
         self.assertEqual(1, stackless.getruncount())
     finally:
         while main_tasklet is not main_tasklet.prev:
             main_tasklet.prev.kill()
예제 #33
0
    def testChannelMultiReceiver(self):

        c = stackless.channel()
        xs = []
        def ch(i):
            xs.append((i, c.receive()))

        child1 = stackless.tasklet(ch)(1)
        child2 = stackless.tasklet(ch)(2)

        self.assertEquals(3, stackless.getruncount()) #main

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals(1, stackless.getruncount()) #main

        self.assertEquals([], xs)
예제 #34
0
    def setUp(self):
        self._ran_AsTaskletTestCase_setUp = True
        if stackless.enable_softswitch(None):
            self.assertEqual(stackless.current.nesting_level, 0)

        super(StacklessTestCase, self).setUp()  # yes, its intended: call setUp on the grand parent class
        self.assertEqual(stackless.getruncount(), 1, "Leakage from other tests, with %d tasklets still in the scheduler" % (stackless.getruncount() - 1))
        if withThreads:
            self.assertEqual(threading.activeCount(), 1, "Leakage from other threads, with %d threads running (1 expected)" % (threading.activeCount()))
def a_main_tasklet():
    threadID = 2

    stackless.tasklet(ManageSleepingTasklets)(threadID)

    stackless.tasklet(looping_tasklet)(threadID, 1)

    print threadID, "runcount.1", stackless.getruncount()
    stackless.run()
예제 #36
0
def a_main_tasklet():
    threadID = 2

    stackless.tasklet(ManageSleepingTasklets)(threadID)

    stackless.tasklet(looping_tasklet)(threadID, 1)

    print threadID, "runcount.1", stackless.getruncount()
    stackless.run()
예제 #37
0
        def child1():
            i = 0
            while True:
                i += 1
                #print 'c', i
                r.rc1 = stackless.getruncount()
                r.cur1 = stackless.getcurrent()
                r.blocked1 = r.cur1.blocked
                r.alive1 = r.cur1.alive

                try:
                    stackless.schedule()
                except TaskletExit:
                    #print 'kill'
                    r.rc2 = stackless.getruncount()
                    r.cur2 = stackless.getcurrent()
                    r.blocked2 = r.cur2.blocked
                    r.alive2 = r.cur2.alive
                    raise
예제 #38
0
    def testChannelMultiReceiver(self):

        c = stackless.channel()
        xs = []

        def ch(i):
            xs.append((i, c.receive()))

        child1 = stackless.tasklet(ch)(1)
        child2 = stackless.tasklet(ch)(2)

        self.assertEquals(3, stackless.getruncount())  #main

        while stackless.getruncount() > 1:
            stackless.schedule()

        self.assertEquals(1, stackless.getruncount())  #main

        self.assertEquals([], xs)
예제 #39
0
 def runner_func():
     # run the watchdog long enough to start t1 and t2
     stackless.run(150,
                   soft=soft,
                   totaltimeout=True,
                   ignore_nesting=True)
     if stackless.getruncount():
         self.done += 1  # we were interrupted
     t1.kill()
     t2.kill()
예제 #40
0
        def child1():
            i = 0
            while True:
                i += 1
                #print 'c', i
                r.rc1 = stackless.getruncount()
                r.cur1 = stackless.getcurrent()
                r.blocked1 = r.cur1.blocked
                r.alive1 = r.cur1.alive

                try:
                    stackless.schedule()
                except TaskletExit:
                    #print 'kill'
                    r.rc2 = stackless.getruncount()
                    r.cur2 = stackless.getcurrent()
                    r.blocked2 = r.cur2.blocked
                    r.alive2 = r.cur2.alive
                    raise
예제 #41
0
    def testTempval(self):
        def Worker(items):
            items.append(stackless.schedule())
            items.append(stackless.schedule(None))
            items.append(stackless.schedule('foo'))
            items.append(stackless.schedule(42))

        items = []
        tasklet_obj = stackless.tasklet(Worker)(items)
        self.assertEqual(None, tasklet_obj.tempval)
        self.assertEqual([], items)
        stackless.current.tempval = 5
        self.assertEqual(stackless.getcurrent(), stackless.schedule())
        self.assertEqual(None, stackless.current.tempval)
        self.assertEqual(tasklet_obj, tasklet_obj.tempval)
        self.assertEqual([], items)
        stackless.schedule()
        self.assertEqual(None, tasklet_obj.tempval)
        self.assertEqual([tasklet_obj], items)
        stackless.schedule()
        self.assertEqual('foo', tasklet_obj.tempval)
        self.assertEqual([tasklet_obj, None], items)
        tasklet_obj.tempval = False
        stackless.schedule()
        self.assertEqual([tasklet_obj, None, False], items)
        self.assertEqual(42, tasklet_obj.tempval)
        stackless.schedule()
        self.assertEqual([tasklet_obj, None, False, 42], items)
        # Upon TaskletExit.
        self.assertEqual(None, tasklet_obj.tempval)
        self.assertEqual(1, stackless.getruncount())
        self.assertEqual(stackless.getcurrent(), stackless.schedule())
        self.assertEqual(None, stackless.current.tempval)
        self.assertEqual(43, stackless.schedule(43))
        # This seems to be a strange Stackless quirk, this should be 43.
        self.assertEqual(None, stackless.getcurrent().tempval)
        self.assertEqual(54, stackless.schedule_remove(54))
        self.assertEqual(None, stackless.current.tempval)

        def Worker2(items, main_tasklet):
            items.append(stackless.getcurrent().tempval)
            items.append(stackless.schedule(44))
            items.append(stackless.current.tempval)
            main_tasklet.insert()

        del items[:]
        stackless.tasklet(Worker2)(items, stackless.getcurrent())
        self.assertEqual(55, stackless.schedule_remove(55))
        self.assertEqual(None, stackless.current.tempval)
        self.assertEqual([None, 44, None], items)

        self.assertRaisesStr(AssertionError, '', stackless.schedule,
                             stackless.bomb(AssertionError))
        self.assertRaisesStr(AssertionError, 'foo', stackless.schedule,
                             stackless.bomb(AssertionError, 'foo'))
예제 #42
0
  def testTempval(self):
    def Worker(items):
        items.append(stackless.schedule())
        items.append(stackless.schedule(None))
        items.append(stackless.schedule('foo'))
        items.append(stackless.schedule(42))
  
    items = []
    tasklet_obj = stackless.tasklet(Worker)(items)
    self.assertEqual(None, tasklet_obj.tempval)
    self.assertEqual([], items)
    stackless.current.tempval = 5
    self.assertEqual(stackless.getcurrent(), stackless.schedule())
    self.assertEqual(None, stackless.current.tempval)
    self.assertEqual(tasklet_obj, tasklet_obj.tempval)
    self.assertEqual([], items)
    stackless.schedule()
    self.assertEqual(None, tasklet_obj.tempval)
    self.assertEqual([tasklet_obj], items)
    stackless.schedule()
    self.assertEqual('foo', tasklet_obj.tempval)
    self.assertEqual([tasklet_obj, None], items)
    tasklet_obj.tempval = False
    stackless.schedule()
    self.assertEqual([tasklet_obj, None, False], items)
    self.assertEqual(42, tasklet_obj.tempval)
    stackless.schedule()
    self.assertEqual([tasklet_obj, None, False, 42], items)
    # Upon TaskletExit.
    self.assertEqual(None, tasklet_obj.tempval)
    self.assertEqual(1, stackless.getruncount())
    self.assertEqual(stackless.getcurrent(), stackless.schedule())
    self.assertEqual(None, stackless.current.tempval)
    self.assertEqual(43, stackless.schedule(43))
    # This seems to be a strange Stackless quirk, this should be 43.
    self.assertEqual(None, stackless.getcurrent().tempval)
    self.assertEqual(54, stackless.schedule_remove(54))
    self.assertEqual(None, stackless.current.tempval)

    def Worker2(items, main_tasklet):
      items.append(stackless.getcurrent().tempval)
      items.append(stackless.schedule(44))
      items.append(stackless.current.tempval)
      main_tasklet.insert()

    del items[:]
    stackless.tasklet(Worker2)(items, stackless.getcurrent())
    self.assertEqual(55, stackless.schedule_remove(55))
    self.assertEqual(None, stackless.current.tempval)
    self.assertEqual([None, 44, None], items)

    self.assertRaisesStr(AssertionError, '', stackless.schedule,
                         stackless.bomb(AssertionError))
    self.assertRaisesStr(AssertionError, 'foo', stackless.schedule,
                         stackless.bomb(AssertionError, 'foo'))
예제 #43
0
파일: bajo_crash.py 프로젝트: d11/rts
def Start():
    try:
        a = A()
        stackless.tasklet(a.bug)()
        stackless.schedule()
        raise Exception

    finally:
        this = stackless.getcurrent()
        while stackless.getruncount() > 1:
            this.next.kill()
예제 #44
0
def Start():
    try:
        a = A()
        stackless.tasklet(a.bug)()
        stackless.schedule()
        raise Exception

    finally:
        this = stackless.getcurrent()
        while stackless.getruncount() > 1:
            this.next.kill()
예제 #45
0
def run():
    """Run a bunch of tasks, until they all complete"""
    # have to add error handling like above
    while stackless.getruncount() > 1:
        try:
            # running until the end or until something happens
            victim = stackless.run_watchdog(MAX_TIMESLICE)
            if victim:
                print "THIS THREAD WAS PROBABLY STUCK IN AN INFINITE LOOP AND HAS BEEN NUKED!!!",
            # todo: kill the task cleanly
        except Exception, e:
            print "Exception occoured:", e
예제 #46
0
def run():
    """Run a bunch of tasks, until they all complete"""
    # have to add error handling like above
    while stackless.getruncount() > 1:
        try:
            # running until the end or until something happens
            victim = stackless.run_watchdog(MAX_TIMESLICE)
            if victim:
                print "THIS THREAD WAS PROBABLY STUCK IN AN INFINITE LOOP AND HAS BEEN NUKED!!!",
            # todo: kill the task cleanly
        except Exception, e:
            print "Exception occoured:", e
예제 #47
0
    def getRunCount(self):
        """Same as stackless.getruncount but takes sleeping tasklets into account

        Will return 1 when only the main tasklet is left, the timeKeeper tasklet
        will not be counted since it is not a client tasklet.

        For consideration: If stackless.getruncount() returns 2
            only the main tasklet and the TimeKeeper tasklets are currently
            running. This might then be a good place to sleep for one millisecond
            to reduce CPU load
        """
        runCount = stackless.getruncount() + self.timeKeeper.getSleeperCount()
        return (runCount - 1)  # subtract the timeKeeper tasklet
예제 #48
0
    def getRunCount(self):
        """Same as stackless.getruncount but takes sleeping tasklets into account

        Will return 1 when only the main tasklet is left, the timeKeeper tasklet
        will not be counted since it is not a client tasklet.

        For consideration: If stackless.getruncount() returns 2
            only the main tasklet and the TimeKeeper tasklets are currently
            running. This might then be a good place to sleep for one millisecond
            to reduce CPU load
        """
        runCount = stackless.getruncount() + self.timeKeeper.getSleeperCount()
        return (runCount - 1)  # subtract the timeKeeper tasklet
예제 #49
0
def eventLoop():
    global loop_running
    global event_errors

    while sockets.values():
        # If there are other tasklets scheduled, then use the nonblocking loop,
        # else, use the blocking loop
        if stackless.getruncount() > 2:  # main tasklet + this one
            event.loop(True)
        else:
            event.loop(False)
        stackless.schedule()
    loop_running = False
예제 #50
0
def stacklessThread(requestChannel):

    cgiTasklet = Cgi("cgiTasklet-1", requestChannel)
    stackless.tasklet(cgiTasklet.execute)()
    """
    in this example, if the tick tasklet did not exist,
    Stackless would complain about deadlock since the
    last runnable tasklet (cgiTasklet) would be blocked
    """
    stackless.tasklet(tick)()

    while (stackless.getruncount() > 1):
        stackless.schedule()
예제 #51
0
def eventLoop():
    global loop_running
    global event_errors
    
    while sockets.values():
        # If there are other tasklets scheduled, then use the nonblocking loop,
        # else, use the blocking loop
        if stackless.getruncount() > 2: # main tasklet + this one
            event.loop(True)
        else:
            event.loop(False)
        stackless.schedule()
    loop_running = False
def stacklessThread(requestChannel):

    cgiTasklet = Cgi("cgiTasklet-1", requestChannel)
    stackless.tasklet(cgiTasklet.execute)()

    """
    in this example, if the tick tasklet did not exist,
    Stackless would complain about deadlock since the
    last runnable tasklet (cgiTasklet) would be blocked
    """
    stackless.tasklet(tick)()

    while (stackless.getruncount() > 1):
        stackless.schedule()
def a_main_tasklet():
    global running

    threadID = GetThreadID()

    for i in range(3):
        stackless.tasklet(looping_tasklet)(threadID, i+1)

    # We need to catch the keyboard interrupt and signal the other thread to exit.
    try:
        print threadID, "start: runcount", stackless.getruncount()

        # Do a preliminary run to get some tasklets in the scheduler or some
        # sleeping tasklets in place, otherwise we would not run at all.
        stackless.run()

        # Now we should be set to run until we are done.
        while running and (stackless.getruncount() > 1 or sleepCountByThread.get(threadID, 0)):
            print threadID, "start: runcount", stackless.getruncount(), "sleepcount", sleepCountByThread.get(threadID, 0)
            stackless.run()
        print threadID, "stop: runcount", stackless.getruncount(), "sleepcount", sleepCountByThread.get(threadID, 0)
    except:
        running = False
        raise
예제 #54
0
def ManageSleepingTasklets():
    while True:
        if len(sleepingTasklets):
            endTime = sleepingTasklets[0][0]
            if endTime <= time.time():
                channel = sleepingTasklets[0][1]
                del sleepingTasklets[0]
                # We have to send something, but it doesn't matter what as it is not used.
                channel.send(None)
            elif stackless.getruncount() == 1:
                # We are the only tasklet running, the rest are blocked on channels sleeping.
                # We can call time.sleep until the first awakens to avoid a busy wait.
                delay = endTime - time.time()
                #print "wait delay", delay
                time.sleep(max(delay,0))
        stackless.schedule()
예제 #55
0
 def tearDown(self):
     # Tasklets created in pickling tests can be left in the scheduler when they finish.  We can feel free to
     # clean them up for the tests.  Any tests that expect to exit with no leaked tasklets should do explicit
     # assertions to check.
     mainTasklet = stackless.getmain()
     current = mainTasklet.next
     while current is not mainTasklet:
         next = current.next
         current.kill()
         current = next
     self.assertEqual(stackless.getruncount(), 1, "Leakage from this test, with %d tasklets still in the scheduler" % (stackless.getruncount() - 1))
     if threading.activeCount() > 1:
         activeThreads = threading.enumerate()
         activeThreads.remove(threading.currentThread())
         activeThreads[0].join(0.5)
         if threading.activeCount() > 1:
             self.assertEqual(threading.activeCount(), 1, "Leakage from other threads, with %d threads running (1 expected)" % (threading.activeCount()))
예제 #56
0
def ManageSleepingTasklets(threadID):
    global running

    _locals.sleepingTasklets = []
    while running:
        if len(_locals.sleepingTasklets):
            endTime = _locals.sleepingTasklets[0][0]
            if endTime <= time.time():
                channel = _locals.sleepingTasklets[0][1]
                del _locals.sleepingTasklets[0]
                # We have to send something, but it doesn't matter what as it is not used.
                channel.send(None)
        elif stackless.getruncount() == 1:
            # Give up if there are no more sleeping tasklets.  Otherwise the two
            # threads keep on running endlessly.
            break
        stackless.schedule()
예제 #57
0
  def testRun(self):
    stackless.tasklet(stackless.schedule)(42)
    tasklet_obj = stackless.tasklet(stackless.schedule)(43)
    stackless.tasklet(stackless.schedule)(44)
    self.assertEqual(4, stackless.getruncount())
    self.assertEqual(None, tasklet_obj.run())
    self.assertEqual(4, stackless.getruncount())
    self.assertEqual(50, stackless.schedule(50))
    self.assertEqual(2, stackless.getruncount())
    self.assertEqual(51, stackless.schedule(51))
    self.assertEqual(1, stackless.getruncount())

    def Bomber(tasklet_obj, msg):
      tasklet_obj.tempval = stackless.bomb(AssertionError, msg)

    stackless.tasklet(Bomber)(stackless.current, 'foo')
    stackless.tasklet(Bomber)(stackless.current, 'bar')
    self.assertRaisesStr(AssertionError, 'bar', stackless.schedule)

    def Setter(tasklet_obj, msg):
      tasklet_obj.tempval = msg

    stackless.tasklet(Setter)(stackless.current, 'food')
    stackless.tasklet(Setter)(stackless.current, 'bard')
    self.assertEqual('bard', stackless.schedule())
    self.assertEqual(1, stackless.getruncount())
    
    stackless.tasklet(Setter)(stackless.current, 'fooe')
    tasklet_obj = stackless.tasklet(Setter)(stackless.current, 'bare')
    stackless.tasklet(Setter)(stackless.current, 'baze')
    stackless.current.tempval = 'tv'
    self.assertEqual('tv', stackless.current.run())
    self.assertEqual('baze', tasklet_obj.run())
    self.assertEqual('fooe', stackless.schedule())
    self.assertEqual(1, stackless.getruncount())

    stackless.current.tempval = 50
    tasklet_obj = stackless.tasklet(lambda: 0)()
    self.assertEqual(50, tasklet_obj.run())
    self.assertEqual(None, stackless.current.tempval)
    self.assertEqual(1, stackless.getruncount())

    stackless.current.tempval = 51
    self.assertEqual(51, stackless.current.run())
    self.assertEqual(None, stackless.current.tempval)
 def run(self):
     """Main process function."""
     while True:
         # Check whether any new tasks should be scheduled.
         while not self.__scheduling_queue.empty():
             try:
                 task_module, task_input, execid = self.__scheduling_queue.get_nowait()
                 stackless.tasklet(self.perform_task)(task_module, task_input, execid)
             except QueueEmptyException:
                 break
                             
         # Schedule currently active tasklets - if any.
         if stackless.getruncount() != 1:
             # Run for the next STEP_SIZE instructions.
             tasklet = stackless.run(CoreScheduler.STEP_SIZE)
             if tasklet:
                 # Check this task against the "sinners" registry.
                 if tasklet in self.__sinners:
                     # The tasklet has been added to sinners already. Check how many times it has been pre-empted.
                     if self.__sinners[tasklet] == CoreScheduler.MAX_SINS:
                         # This sinner must be killed. Try to kill it nicely by raising a TaskletExit exception.
                         print "killing tasklet" #DEBUG
                         self.__sinners[tasklet] = -1
                         stackless.tasklet(self.kill_tasklet)(tasklet)
                     elif self.__sinners[tasklet] == -1:
                         # The bastard caught the TaskletExit exception! Silently remove him from the runables queue.
                         print "bastard!" #DEBUG
                         self.__sinners.pop(tasklet)
                     else:
                         # Give him another chance.
                         self.__sinners[tasklet] += 1
                         tasklet.insert()
                 else: # if tasklet in self.__sinners
                     # Insert the tasklet into the sinners registry.
                     self.__sinners[tasklet] = 1
                     tasklet.insert()
         else:
             # Otherwise sleep for a little while.
             sleep(CoreScheduler.SLEEP_TIME)
예제 #59
0
파일: entry.py 프로젝트: harkal/sylphis3d
def entry():
    import sylphisexceptions
    global sde

    #sde = SDEThread()

    #global debugger
    #from Debugger import Debugger
    #debugger = Debugger()
    
    #global rpcs
    #rpcs = rpcserver.CRPCServer(('127.0.0.1', 8000))
    #rpcs.register_instance(debugger)
    
    #debugger.setIdleCall(rpcs.handle_request)
    #debugger.enable_trace()
    mthread.new("entry()", _entry)
    
    if 0:
        import profile
        print "profiling"
        p = profile.Profile()
        p.runcall(stackless.run)
        p.print_stats()
    else:
        retry = True
        while retry:
            try:
                stackless.run()
                retry = False
            except Exception, e:
                ## If there are more microthreads running
                ## don't terminate. Just print out the exception
                ## and continue scheduling
                if stackless.getruncount() <= 2:
                    retry = False
                traceback.print_exc()
예제 #60
0
    def watcher(old):
        while stackless.getruncount() > 1:
            stackless.schedule()

        old.insert()