Пример #1
0
    def __init__(self, host, port):
        self.host = host
        self.port = port
                
        self.userIndex = weakref.WeakValueDictionary()

        stackless.tasklet(self.Run)()
Пример #2
0
    def test_try_acquire(self):
        have = []

        def func(n, counter):
            successes = 0
            while successes < n:
                if self.lock.try_acquire():
                    try:
                        successes += 1
                        counter[0] += 1
                        self.assertEqual(have, [])
                        have.append(stackless.getcurrent())
                        for i in xrange(random.randint(1, 5)):
                            stackless.schedule()

                        self.assertEqual(have, [stackless.getcurrent()])
                        have.remove(stackless.getcurrent())
                    finally:
                        self.lock.release()

                else:
                    counter[1] += 1
                stackless.schedule()

        c0 = [0, 0]
        c1 = [0, 0]
        n = 10
        stackless.tasklet(func)(n, c0)
        stackless.tasklet(func)(n, c1)
        Run()
        self.assertEqual(c0[0], n)
        self.assertEqual(c1[0], n)
        self.assertTrue(c0[1] > 0)
        self.assertTrue(c1[1] > 0)
Пример #3
0
    def testProducerConsumer(self):

        def producer(n, slot):
            for i in xrange(n):
                while True:
                    with self.lock:
                        if not slot:
                            slot.append(i)
                            self.lock.notify()
                            break
                    stackless.schedule()

        def consumer(n, slot, result):
            for i in xrange(n):
                with self.lock:
                    while not slot:
                        self.lock.wait()

                    result.append(slot[0])
                    del slot[:]

        theslot = []
        result = []
        n = 5
        stackless.tasklet(producer)(n, theslot)
        stackless.tasklet(consumer)(n, theslot, result)
        Run()
        self.assertEqual(result, range(n))
Пример #4
0
 def handle_accept(self):
     if self.acceptChannel.balance < 0:
         currentSocket, clientAddress = asyncore.dispatcher.accept(self)
         currentSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
         # Give them the asyncore based socket, not the standard one.
         currentSocket = self.wrap_accept_socket(currentSocket)
         stackless.tasklet(self.acceptChannel.send)((currentSocket, clientAddress))
Пример #5
0
    def testProducerConsumer2(self):
        lock = RLock()
        producerReady = Condition(lock)
        consumerReady = Condition(lock)

        def producer(n, slot):
            for i in xrange(n):
                with producerReady:
                    while slot:
                        producerReady.wait()

                    slot.append(i)
                    consumerReady.notify()

        def consumer(n, slot, result):
            for i in xrange(n):
                with consumerReady:
                    while not slot:
                        consumerReady.wait()

                    result.append(slot[0])
                    del slot[:]
                    producerReady.notify()

        theslot = []
        result = []
        n = 5
        stackless.tasklet(producer)(n, theslot)
        stackless.tasklet(consumer)(n, theslot, result)
        Run()
        self.assertEqual(result, range(n))
Пример #6
0
    def test_construction(self):
        output = []
        def print_(*args):
            output.append(args)

        def aCallable(value):
            print_("aCallable:", value)

        task = stackless.tasklet(aCallable)
        task.setup('Inline using setup')

        stackless.run()
        assert output == [("aCallable:", 'Inline using setup')]


        del output[:]
        task = stackless.tasklet(aCallable)
        task('Inline using ()')

        stackless.run()
        assert output == [("aCallable:", 'Inline using ()')]
        
        del output[:]
        task = stackless.tasklet()
        task.bind(aCallable)
        task('Bind using ()')

        stackless.run()
        assert output == [("aCallable:", 'Bind using ()')]
Пример #7
0
    def test_simple_channel(self):
        output = []
        def print_(*args):
            output.append(args)
            
        def Sending(channel):
            print_("sending")
            channel.send("foo")

        def Receiving(channel):
            print_("receiving")
            print_(channel.receive())

        ch=stackless.channel()

        task=stackless.tasklet(Sending)(ch)

        # Note: the argument, schedule is taking is the value,
        # schedule returns, not the task that runs next

        #stackless.schedule(task)
        stackless.schedule()
        task2=stackless.tasklet(Receiving)(ch)
        #stackless.schedule(task2)
        stackless.schedule()

        stackless.run()

        assert output == [('sending',), ('receiving',), ('foo',)]
Пример #8
0
    def test_except(self):
        rlist = []
        def f():
            rlist.append('f')
            return 1/0

        def g():
            rlist.append('bg')
            stackless.schedule()
            rlist.append('ag')

        def h():
            rlist.append('bh')
            stackless.schedule()
            rlist.append('ah')

        tg = stackless.tasklet(g)()
        tf = stackless.tasklet(f)()
        th = stackless.tasklet(h)()

        try:
            stackless.run()
        # cheating, can't test for ZeroDivisionError
        except Exception as e:
            rlist.append('E')
        stackless.schedule()
        stackless.schedule()

        assert rlist == "bg f E bh ag ah".split()
Пример #9
0
    def test_except_full(self):
        rlist = []
        def f():
            rlist.append('f')
            return 1/0

        def g():
            rlist.append('bg')
            stackless.schedule()
            rlist.append('ag')

        def h():
            rlist.append('bh')
            stackless.schedule()
            rlist.append('ah')

        tg = stackless.tasklet(g)()
        tf = stackless.tasklet(f)()
        th = stackless.tasklet(h)()

        try:
            stackless.run()
        except ZeroDivisionError:
            rlist.append('E')
        stackless.schedule()
        stackless.schedule()

        assert rlist == "bg f E bh ag ah".split()
    def TestMonkeyPatchUDP(address):
        # replace the system socket with this module
        install()
        try:
            def UDPServer(address):
                listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
                listenSocket.bind(address)

                # Apparently each call to recvfrom maps to an incoming
                # packet and if we only ask for part of that packet, the
                # rest is lost.  We really need a proper unittest suite
                # which tests this module against the normal socket
                # module.
                print "waiting to receive"
                rdata = ""
                while len(rdata) < 512:
                    data, address = listenSocket.recvfrom(4096)
                    print "received", data, len(data)
                    rdata += data

            def UDPClient(address):
                clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                # clientSocket.connect(address)
                print "sending 512 byte packet"
                sentBytes = clientSocket.sendto("-"+ ("*" * 510) +"-", address)
                print "sent 512 byte packet", sentBytes

            stackless.tasklet(UDPServer)(address)
            stackless.tasklet(UDPClient)(address)
            stackless.run()
        finally:
            uninstall()
Пример #11
0
    def test_scheduling_cleanup(self):
        rlist = []
        def f():
            rlist.append('fb')
            stackless.schedule()
            rlist.append('fa')

        def g():
            rlist.append('gb')
            stackless.schedule()
            rlist.append('ga')

        def h():
            rlist.append('hb')
            stackless.schedule()
            rlist.append('ha')

        tf = stackless.tasklet(f)()
        tg = stackless.tasklet(g)()
        th = stackless.tasklet(h)()

        rlist.append('mb')
        stackless.run()
        rlist.append('ma')

        assert rlist == 'mb fb gb hb fa ga ha ma'.split()
Пример #12
0
 def _reply_to_connections(self):
     """
     sends any queues messages
     :return:
     """
     while not self.outbound_message_queue.empty():
         stackless.tasklet(self._send_message)
 def handle_accept(self):
     if self.acceptChannel and self.acceptChannel.balance < 0:
         t = asyncore.dispatcher.accept(self)
         if t is None:
             return
         t[0].setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
         stackless.tasklet(self.acceptChannel.send)(t)
Пример #14
0
	def startserver( f ):
		while 1:
			newsock = f.accept()
			print "+++ new", newsock
			SL.tasklet(startwork)(newsock)
			import time
			time.sleep(1)
Пример #15
0
def mainloop():
    tylog.info('Main loop begin.')
    stackless.tasklet(reactor.run)()
    reactor.callLater(0, stackless.schedule)
    stackless.run()
    tylog.info('Main loop over.')
    sys.exit(0)
Пример #16
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()
Пример #17
0
    def __init__(self,name,circle):
        self.name = name
        self.circle = circle
        circle.append(self)
        self.channel = stackless.channel()

        stackless.tasklet(self.messageLoop)()
Пример #18
0
  def testChannelReceiveLinkedList(self):
    """Test how the linked lists are formed when blocked on a channel."""
    stackless.is_slow_prev_next_ok = True
    channel_obj = stackless.channel()
    self.assertEqual(None, channel_obj.queue)
    tasklet1 = stackless.tasklet(channel_obj.receive)()
    assert tasklet1.next is stackless.getcurrent()

    stackless.schedule()
    self.assertEqual(tasklet1, channel_obj.queue)
    assert tasklet1.next is None
    assert tasklet1.prev is None

    tasklet2 = stackless.tasklet(channel_obj.receive)()
    self.assertEqual(True, tasklet1.blocked)
    self.assertEqual(True, tasklet1.scheduled)
    self.assertEqual(channel_obj, tasklet1._channel)
    self.assertEqual(False, tasklet2.blocked)
    self.assertEqual(True, tasklet2.scheduled)
    self.assertEqual(None, tasklet2._channel)
    stackless.schedule()
    self.assertEqual(channel_obj, tasklet2._channel)
    assert tasklet1.next is tasklet2
    assert tasklet1.prev is None

    tasklet3 = stackless.tasklet(channel_obj.receive)()
    self.assertEqual(tasklet1, channel_obj.queue)
    stackless.schedule()
    self.assertEqual(tasklet1, channel_obj.queue)
    assert tasklet1.next is tasklet2
    assert tasklet2.next is tasklet3
    assert tasklet3.next is None
    assert tasklet1.prev is None
    assert tasklet2.prev is tasklet1
    assert tasklet3.prev is tasklet2
Пример #19
0
  def testRunnablesOrderAtKill(self):
    def Safe(items):
      try:
        items.append('start')
        stackless.schedule()
      except ValueError:
        items.append('caught')

    stackless.schedule()
    tasklet1 = stackless.tasklet(lambda: 1 / 0)()
    items = []
    tasklet2 = stackless.tasklet(Safe)(items)
    tasklet2.run()
    assert 'start' == ','.join(items)
    tasklet3 = stackless.tasklet(lambda: 1 / 0)()
    tasklet2.remove()
    tasklet2.remove()
    tasklet2.raise_exception(ValueError)
    assert 'start,caught' == ','.join(items)
    assert tasklet1.alive
    assert not tasklet2.alive
    assert tasklet3.alive
    tasklet1.remove()
    tasklet1.kill()  # Don't run tasklet3.
    tasklet3.kill()
    tasklet2.kill()
Пример #20
0
    def __init__(self):
        Object.__init__(self)

        self.components = []
        self.componentsByPosition = {}

        stackless.tasklet(self.ManageFire)()
Пример #21
0
  def testKillJumpsInRunnablesList(self):
    items = []

    def Worker():
      items.append('worker')

    def Dead():
      items.append('dead')

    self.assertEqual(stackless.main, stackless.current)
    self.assertEqual(stackless.main, stackless.current.next)
    self.assertEqual(stackless.main, stackless.current.prev)
    dead_tasklet = stackless.tasklet(Dead)()
    self.assertEqual(stackless.main, stackless.current)
    self.assertEqual(dead_tasklet, stackless.current.next)
    self.assertEqual(stackless.main, stackless.current.next.next)
    self.assertEqual(dead_tasklet, stackless.current.prev)
    self.assertEqual(stackless.main, stackless.current.prev.prev)
    worker_tasklet = stackless.tasklet(Worker)()
    self.assertEqual(stackless.main, stackless.current)
    self.assertEqual(dead_tasklet, stackless.current.next)
    self.assertEqual(worker_tasklet, stackless.current.next.next)
    self.assertEqual(stackless.main, stackless.current.next.next.next)
    self.assertEqual(worker_tasklet, stackless.current.prev)
    self.assertEqual(dead_tasklet, stackless.current.prev.prev)
    self.assertEqual(stackless.main, stackless.current.prev.prev.prev)
    items.append('before')
    dead_tasklet.kill()
    self.assertEqual(stackless.main, stackless.current)
    self.assertEqual(stackless.main, stackless.current.next)
    self.assertEqual(stackless.main, stackless.current.prev)
    items.append('after')
    self.assertEqual('before,worker,after', ','.join(items))
Пример #22
0
    def TestMonkeyPatchUDP(address):
        install()
        try:

            def UDPServer(address):
                listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
                listenSocket.bind(address)
                print 'waiting to receive'
                rdata = ''
                while len(rdata) < 512:
                    data, address = listenSocket.recvfrom(4096)
                    print 'received', data, len(data)
                    rdata += data

            def UDPClient(address):
                clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                print 'sending 512 byte packet'
                sentBytes = clientSocket.sendto('-' + '*' * 510 + '-', address)
                print 'sent 512 byte packet', sentBytes

            stackless.tasklet(UDPServer)(address)
            stackless.tasklet(UDPClient)(address)
            stackless.run()
        finally:
            uninstall()
Пример #23
0
def runEventLoop():
    global loop_running
    if not loop_running:
        event.init()
        event.signal(2, die)
        stackless.tasklet(eventLoop)()
        loop_running = True
Пример #24
0
    def TestMonkeyPatchUDP(address):
        # replace the system socket with this module
        #oldSocket = sys.modules["socket"]
        #sys.modules["socket"] = __import__(__name__)
        install()
        try:
            def UDPServer(address):
                listenSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
                listenSocket.bind(address)

                # Apparently each call to recvfrom maps to an incoming
                # packet and if we only ask for part of that packet, the
                # rest is lost.  We really need a proper unittest suite
                # which tests this module against the normal socket
                # module.
                print "waiting to receive"
                data, address = listenSocket.recvfrom(256)
                print "received", data, len(data)
                if len(data) != 256:
                    raise StandardError("Unexpected UDP packet size")

            def UDPClient(address):
                clientSocket = stdsocket.socket(AF_INET, SOCK_DGRAM)
                # clientSocket.connect(address)
                print "sending 512 byte packet"
                sentBytes = clientSocket.sendto("-"+ ("*" * 510) +"-", address)
                print "sent 512 byte packet", sentBytes

            stackless.tasklet(UDPServer)(address)
            stackless.tasklet(UDPClient)(address)
            stackless.run()
        finally:
            #sys.modules["socket"] = oldSocket
            uninstall()
Пример #25
0
    def test_with_channel(self):
        rlist = []
        def f(outchan):
            for i in range(10):
                rlist.append('s%s' % i)
                outchan.send(i)
            outchan.send(-1)

        def g(inchan):
            while 1:
                val = inchan.receive()
                if val == -1:
                    break
                rlist.append('r%s' % val)

        ch = stackless.channel()
        t1 = stackless.tasklet(f)(ch)
        t2 = stackless.tasklet(g)(ch)

        stackless.run()

        assert len(rlist) == 20
        for i in range(10):
            (s,r), rlist = rlist[:2], rlist[2:]
            assert s == 's%s' % i
            assert r == 'r%s' % i
Пример #26
0
	def handle_read(self):
		try:
			conn, addr = self.socket.accept()
		except:
			pass
		else:
			tasklet(self.handler)(conn, addr)
Пример #27
0
 def __init__(self, count):
     self.count = count
     self.channel = stackless.channel()
     self.valarr = [[ -1 for i in range(self.count) ] for k in range(self.count)]
     self.pings = []
     stackless.tasklet(self.handle)()
     self.initboard()
Пример #28
0
    def handle_write(self):
        if len(self.writeQueue):
            channel, flags, data = self.writeQueue[0]
            del self.writeQueue[0]

            def asyncore_send(self, data, flags = 0):
                try:
                    result = self.socket.send(data, flags)
                    return result
                except stdsocket.error as why:
                    if why.args[0] == EWOULDBLOCK:
                        return 0
                    channel.send_exception(why.__class__, *why.args)
                    if why.args[0] in (ECONNRESET,
                     ENOTCONN,
                     ESHUTDOWN,
                     ECONNABORTED):
                        self.handle_close()
                    return 0

            nbytes = asyncore_send(self, data, flags)
            if channel.balance < 0:
                channel.send(nbytes)
        elif len(self.sendToBuffers):
            data, address, channel, oldSentBytes = self.sendToBuffers[0]
            sentBytes = self.socket.sendto(data, address)
            totalSentBytes = oldSentBytes + sentBytes
            if len(data) > sentBytes:
                self.sendToBuffers[0] = (data[sentBytes:],
                 address,
                 channel,
                 totalSentBytes)
            else:
                del self.sendToBuffers[0]
                stackless.tasklet(channel.send)(totalSentBytes)
Пример #29
0
    def Run(self):
        self.example = Planet(9235632, 6378000.0, 10000.0, 0.15)
        return
        spherical = False
        import renderer
        #spherical = True#False
        def getV(x,y):
            y = (y-90)/180.0 * math.pi
            x = x/180.0 * math.pi
            #print y,x
            vec = math.sin(y)*math.cos(x), math.sin(y)*math.sin(x), math.cos(y)
            sorrows.terrain.SetSeed(self.example.seed)

            height = sorrows.terrain.fBm(8, *vec)
            if height > 0.4:
                color = 1,1,1
            elif height < 0:
                color = 0,0.2*(1.0+height),0.5+abs(height)/2
                height = 0.0
            else:
                color = 0,height,0
            if not spherical:
                return (x,y,height)+color # uncomment for 2d representation
            height =  1 + height*0.2

            return (vec[0] *height, vec[1]*height, vec[2] * height) + color
#            return x,y,self.example.GetSurfaceHeight(vec)-self.example.radius
        stackless.tasklet(renderer.Render, getV)()
def pickle_current_frame():
    result = []  # comment this line to avoid the crash

    def func(current):
        result.append(pickle.dumps(current.frame, -1))
    stackless.tasklet().bind(func, (stackless.current,)).run()
    return result[0]
Пример #31
0
def test():
    ch = MyChannel()
    t2 = stackless.tasklet(f2)(ch)
    t1 = stackless.tasklet(f1)(ch)
    stackless.run()
    return ch
Пример #32
0
 def send_message(self, from_actor, msg):
     self.last_message = msg
     stackless.tasklet(self.respond)(from_actor, msg)
Пример #33
0
 def _execute(self, command):
     self.round_time = 0
     stackless.tasklet(CommandRegistry.dispatch)(command, self)
     self.queued_commands = 0
Пример #34
0
# pingpong_stackless.py
#

# use pypy
import stackless

ping_channel = stackless.channel()
pong_channel = stackless.channel()


def ping():
    while ping_channel.receive():  #blocks here
        print "PING"
        pong_channel.send("from ping")


def pong():
    while pong_channel.receive():
        print "PONG"
        ping_channel.send("from pong")


stackless.tasklet(ping)()
stackless.tasklet(pong)()

# we need to 'prime' the game by sending a start message
# if not, both tasklets will block
stackless.tasklet(ping_channel.send)('startup')

stackless.run()
Пример #35
0
    nextTime = initTime + tickTime
    lastTime = nextTime + timedelta(seconds=15)
    while (1):
        while datetime.now() < nextTime:
            stackless.schedule()
        nextTime += tickTime
        if nextTime > lastTime:
            sys.exit()
        value = str(datetime.now() - initTime)
        channel.send(value)


if __name__ == '__main__':
    print("Execute Main")
    tickerChannel = stackless.channel()
    threadChannel = stackless.channel()
    mainQue = Queue()
    manager = Manager()
    sharedDict = manager.dict()
    connectionDict = dict()
    subtasklets = list()
    #setupGlobals()
    subtasklets.append(stackless.tasklet(makeProcess))
    #subtasklets[-1]()
    subtasklets[-1](mainQue, tickerChannel, threadChannel, sharedDict)
    stackless.tasklet(reactor_run)(connectionDict, subtasklets)
    subtasklets.append(stackless.tasklet(listenToChannel))
    subtasklets[-1](threadChannel, connectionDict)
    subtasklets.append(stackless.tasklet(stacklessTicker))
    subtasklets[-1](tickerChannel, 100000)
    stackless.run()
Пример #36
0
 def _check_running(self):
     if not self.running:
         stackless.tasklet(self.poll)()
         self.running = True
Пример #37
0
                                                  time.time() - st)

    # Creating two dummy files
    newfile = stdfile('test-small.txt', 'w')
    for x in xrange(10000):
        newfile.write(str(x))
    newfile.close()

    newfile2 = stdfile('test-big.txt', 'w')
    for x in xrange(500000):
        newfile2.write(str(x))
    newfile2.close()

    # Launching tasklets to perform the file copy
    for i in xrange(1, 11):
        stackless.tasklet(copyfile)(i, 'test-big.txt', 'big%s.txt' % i)

    for i in xrange(1, 21):
        stackless.tasklet(copyfile)(i, 'test-small.txt', 'sm%s.txt' % i)

    def sl(s):
        st = time.time()
        print "Sleeping for %s seconds" % s
        sleep(s)
        print "returned %s seconds later (real: %s)" % (s, time.time() - st)

    #stackless.tasklet(sl)(3)

    st = time.time()
    stackless.run()
    print "Total time is %s seconds." % (time.time() - st)
Пример #38
0
 def __init__(self):
     self.sleepingTasklets = []
     stackless.tasklet(self.ManageSleepingTasklets)()
Пример #39
0
def autoschedule(bytecodes=None):
    if bytecodes is None:
        bytecodes = sys.getcheckinterval()
    while stackless.runcount > 1:
        schedule_cb(stackless.run(bytecodes))


if __name__ == "__main__":

    def run_atomic(fn, *args, **kwargs):
        current = stackless.current
        oldatomic = current.set_atomic(1)
        rval = fn(*args, **kwargs)
        current.set_atomic(oldatomic)
        return rval

    def print_name(name, count):
        print name, count

    # Helpers
    def runtask(name):
        for ii in xrange(1000):
            if ii % 50:
                run_atomic(print_name, name, ii)

    tasklets = [
        stackless.tasklet(runtask)(name) for name in ["one", "two", "three"]
    ]
    autoschedule()
Пример #40
0
def jump_off(task):
    stackless.tasklet().capture()
    task.remove()
    stackless.schedule()
Пример #41
0
from datetime import date, timedelta, datetime
from General.outsourcingTask import generateSol, stacklessTicker
from General.basicServer import CommunicationServer
from General.newProcess import StacklessProcess, getLock, getSharedObjects

def keepAlive():
    while True:
        stackless.schedule()

def spawnProcess(p):
    p.start()

if __name__ == '__main__':
    mgr, sharedDict, sharedList = getSharedObjects()
    mainLock = getLock()
    responseChannel = stackless.channel()
    storage = dict()
    initialTime = date(3564, 3, 5)
    tickTime = 250000
    timeChannel = stackless.channel()
    #timeChannel = sharedDict
    #timeChannel['date'] = initialTime
    p = StacklessProcess(target=generateSol, lock=mainLock, args=(8, storage, initialTime),
                         listenChannel=timeChannel, responseChannel=responseChannel)
    stackless.tasklet(spawnProcess)(p)
    #stackless.tasklet(generateSol)(8, timeChannel, responseChannel, storage, initialTime)
    stackless.tasklet(stacklessTicker)(timeChannel, tickTime, initialTime)
    s = CommunicationServer(8000, responseChannel)
    stackless.tasklet(s.run)()
    stackless.tasklet(keepAlive)()
    stackless.run()
        try:
            if nesting_level == 0:
                func(bool(self.stackless_getcurrent().nesting_level), case)
            else:
                apply(func, True, case)
        except self.TaskletExit:
            self.checks[index] = checks_killed
            self.out.append(msg_killed)
            raise

    def prep(self, case_nr, index):
        case = self.CASES[case_nr >> 1]
        nesting_level = case_nr & 1
        expect_kill = bool(not stackless.enable_softswitch(None)
                           or nesting_level)
        tlet = stackless.tasklet(self.wrapper)(case[0], index, case_nr,
                                               nesting_level, expect_kill,
                                               case[2])
        self.tasklets.append(("%s, case %d" % (case[0], case_nr), tlet))
        tlet.run()
        assert getattr(tlet, case[1]), "tasklet is not " + case[1]

    def other_thread_main(self):
        assert stackless.main is stackless.current
        self.main = stackless.main
        if self.debug:
            print("other thread started, soft %s, running %s, nl %d" %
                  (stackless.enable_softswitch(None), self.running,
                   self.main.nesting_level))

        if isinstance(self.case, int) and self.case >= 0:
Пример #43
0
def workers_init(workers):
    global channels
    for w in workers:
        print w["post"]
        channels[w["post"]] = stackless.channel()
        stackless.tasklet(worker_run)(w["post"], w)
Пример #44
0
 def __init__(self):
     self.channel = stackless.channel()
     stackless.tasklet(self.process_messeges)()
Пример #45
0
def testChannelConnection(lC, rC):
    stackless.tasklet(testListening)(lC, rC)
Пример #46
0
def Run():
    global bootstrapState

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s;%(name)s;%(levelname)s;%(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    logging.getLogger().name = "default"
    logging.getLogger("namespace").setLevel(logging.INFO)
    logging.getLogger("reloader").setLevel(logging.INFO)

    stackless.getcurrent().block_trap = True

    iniFilename = os.path.join(dirPath, "config.ini")
    if not os.path.exists(iniFilename):
        print "Please copy 'config.ini.base' to 'config.ini' and modify it appropriately."
        sys.exit(0)

    # Monkey-patch in the stackless-compatible sockets.
    import stacklesssocket
    import uthread2
    import stacklesslib.main as stacklesslibmain
    stacklesssocket._schedule_func = lambda delay=0: stacklesslibmain.sleep(delay)
    stacklesssocket._sleep_func = stacklesslibmain.sleep
    stacklesssocket.install()

    # Install the global event handler.
    import __builtin__
    from events import EventHandler
    __builtin__.events = EventHandler()

    # Add the "livecoding" contrib directory to the path.
    livecodingDirPath = os.path.join(dirPath, "contrib", "livecoding")
    if os.path.exists(livecodingDirPath):
        sys.path.append(livecodingDirPath)

    # Register the mudlib and game script directories with the livecoding
    # module.  This will compile and execute them all.
    import reloader
    #gamePath = os.path.join("games", "room - simple")
    gamePath = os.path.join("games", "roguelike")
    gameScriptPath = os.path.join(dirPath, gamePath)
    mudlibScriptPath = os.path.join(dirPath, "mudlib")

    cr = reloader.CodeReloader()
    # Register for code reloading updates of managed classes.
    # Broadcast an event when we receive an update.
    cr.SetClassCreationCallback(OnClassCreation)
    cr.SetClassUpdateCallback(OnClassUpdate)
    cr.SetValidateScriptCallback(OnScriptValidation)
    cr.AddDirectory("mudlib", mudlibScriptPath)
    cr.AddDirectory("game", gameScriptPath)

    import imp
    __builtin__.sorrows = imp.new_module('sorrows')

    from mudlib.services import ServiceService
    svcSvc = ServiceService()
    svcSvc.gameScriptPath = gamePath
    svcSvc.gameDataPath = os.path.join(gamePath, "data")
    svcSvc.Register()
    svcSvc.Start()
    del svcSvc

    stackless.getcurrent().block_trap = False
    bootstrapState = STATE_RUNNING

    manualShutdown = False
    try:
        stacklesslibmain.mainloop.run()
    except KeyboardInterrupt:
        print
        print '** EXITING: Server manually stopping.'
        print
        
        if stackless.runcount > 1:
            print "Scheduled tasklets:", stackless.runcount
            uthread2.PrintTaskletChain(stackless.current)
            print

        if False:
            for entry in stacklesslibmain.event_queue.queue_a:
                print "Sleeping tasklets:"
                uthread2.PrintTaskletChain(uthread.yieldChannel.queue)
                print

            for timestamp, channel in uthread.sleepingTasklets:
                if channel.queue:
                    print "Sleep channel (%d) tasklets:" % id(channel)
                    uthread2.PrintTaskletChain(channel.queue)
                    print

        manualShutdown = True
    finally:
        cr.EndMonitoring()

    bootstrapState = STATE_SHUTDOWN

    if manualShutdown:
        class HelperClass:
            def ShutdownComplete(self):
                stacklesslibmain.mainloop.stop()
                managerTasklet.kill()

        helper = HelperClass()
        events.ShutdownComplete.Register(helper.ShutdownComplete)

        stackless.tasklet(sorrows.services.Stop)()
        # We have most likely killed the stacklesssocket tasklet.
        managerTasklet = stacklesssocket.StartManager()
        stacklesslibmain.mainloop.run()

    logging.info("Shutdown complete")
Пример #47
0
            stackless.tasklet(Worker)(accepted_nbf)
        Log('accept got=%r' % (nbf.Accept(), ))
    finally:
        nbf.Close()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        VERBOSE = True
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(('127.0.0.1', 6666))
    # Reducing this has a strong negative effect on ApacheBench worst-case
    # connection times, as measured with:
    # ab -n 100000 -c 50 http://127.0.0.1:6666/ >ab.stackless3.txt
    # It increases the maximum Connect time from 8 to 9200 milliseconds.
    sock.listen(100)
    Log('listening on %r' % (sock.getsockname(), ))
    new_nbfs = []
    listener_nbf = NonBlockingFile(sock, sock, new_nbfs)
    stackless.tasklet(Listener)(listener_nbf)
    std_nbf = NonBlockingFile(sys.stdin, sys.stdout, new_nbfs)
    stackless.tasklet(ChatWorker)(std_nbf)  # Don't run it right now.
    MainLoop(new_nbfs)
    assert 0, 'unexpected end of main loop'
Пример #48
0
def dummyThread():
    stackless.tasklet(dummyLoop)()
    stackless.run()
Пример #49
0
def sched(ch, graph):
    """Sits in an infinite loop waiting on the channel to recieve data.

    The procedure prolog takes care of sorting the
    input graph into a dependency list and initializing
    the filter tasklets used to construct the graph.

    @param graph: The graph representing the work flow
    @type graph: Python dict organized as a graph struct
    @param ch: The stackless channel to listen on
    @type ch: stackless.channel
    @return: nothing
    """
    edgeList = get_pairlist(graph)
    nodes = topsort(edgeList)
    tasks = []
    inputEdge = Pype()

    for n in nodes:
        # start this microthread
        tasks.append(stackless.tasklet(n.run)())
        try:
            # get this nodes outputs
            edges = graph[n]
        except:
            pass
        else:
            # for each output
            for e in edges:
                e1 = Pype()
                # does this port exist
                if not n.has_port(edges[e][0]):
                    print 'Trying to connect undefined output port', n, edges[
                        e][0]
                    sys.exit(1)

                n.connect_output(edges[e][0], e1)

                # does this port exist
                if not e.has_port(edges[e][1]):
                    print 'Trying to connect undefined input port', e, edges[
                        e][1]
                    sys.exit(1)

                e.connect_input(edges[e][1], e1)

    # Added so that incoming data is fed to every input adapter
    # should check if in exists and create it if it doesn't
    # because a user could remove the input port by accident
    inputEdges = []
    for n in nodes:
        if n.get_type() == 'ADAPTER':
            ie = Pype()
            n.connect_input('in', ie)
            inputEdges.append(ie)
    #nodes[0].connect_input('in', inputEdge)

    while True:
        data = ch.receive()
        for ie in inputEdges:
            ie.send(data)
        #inputEdge.send(data)
        try:
            tasks[0].run()
        except:
            traceback.print_exc()
Пример #50
0
def testListening(lC, rC):
    while True:
        message = lC.receive()
        rC.send(message)

def testSending(rC):
    while True:
        message = rC.receive()
        print(message)

def testChannelConnection(lC, rC):
    stackless.tasklet(testListening)(lC, rC)

def delayedStart(p):
    init = datetime.now() + timedelta(seconds=5)
    while datetime.now() < init:
        stackless.schedule()
    p.start()

if __name__ == '__main__':
    from basicFunctions import stacklessTicker, keepAlive
    lock = getLock()
    lC = stackless.channel()
    rC = stackless.channel()
    #stackless.tasklet(stacklessTicker)(lC, 100000, date(3521, 5, 15))
    stackless.tasklet(keepAlive)()
    stackless.tasklet(testSending)(rC)
    stackless.tasklet(dummyResponse)(lC)
    p = StacklessProcess(target=testChannelConnection, lock=lock, args=(), listenChannel=lC, responseChannel=rC)
    stackless.tasklet(delayedStart)(p)
    stackless.run()
Пример #51
0
    listenSocket = socket(AF_INET, SOCK_STREAM)
    listenSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    listenSocket.bind(address)
    listenSocket.listen(5)

    def handle_echo(_socket, _address):
        while True:
            data = currentSocket.recv(256)
            if data == "":
                print _address, "DISCONNECTED"
                return

            print _address, "READ", data, len(data)
            dlen = currentSocket.send(data)
            print _address, "ECHOD", dlen            

    while True:
        print "Waiting for new connection"
        currentSocket, clientAddress = listenSocket.accept()
        print "Connection", currentSocket.fileno(), "from", clientAddress
        
        stackless.tasklet(handle_echo)(currentSocket, clientAddress)

if __name__ == "__main__":
    StartManager()

    print "STARTED"
    stackless.tasklet(Run)()
    stackless.run()
    print "EXITED"
Пример #52
0
    redis_pool = redis.lazyConnectionPool(host='127.0.0.1',
                                          port=7979,
                                          dbid=0,
                                          poolsize=10)
    print 'before redis pool'
    con = yield redis_pool._connected
    print redis_pool
    gdata["redis_conn"] = redis_pool


class MyProto(FTUDPServerProtocol):
    def func1(self):
        print self
        print self.gdata
        ftredis.runCmd(self.gdata["redis_conn"], "SET", "ZX", 1)
        print "FFFFF"

    def getTaskletFunc(self, pack):
        return self.func1


init()
print encry.ftcode(1111, 'aaaa')
log.loginfo()
mp = MyProto()
print mp
mp.gdata = gdata
reactor.listenUDP(8989, mp)
stackless.tasklet(mainloop)()
stackless.run()
Пример #53
0
 def delayed_callback():
     # call the callback from a worker
     stackless.tasklet(callback)()
Пример #54
0
def StartManager():
    global managerRunning
    if not managerRunning:
        managerRunning = True
        stackless.tasklet(ManageSockets)()
Пример #55
0
 def on_message_from_chan(self):
     line = chan_command_to_client.receive()
     self.sendLine(line)
     stackless.tasklet(self.on_message_from_chan)()
     reactor.callLater(0, stackless.schedule)
Пример #56
0
            secondsToWait -= secondsToActuallyWait
            sleep(secondsToActuallyWait)
        #print "timeout_worker:SLEPT", secondsToWait, args

        if sleeperChannel.balance < 0:
            #print "timeout_worker:WAKEUP", secondsToWait, args
            if args is not None:
                sleeperChannel.send_exception(*args)
            else:
                sleeperChannel.send(None)
        #print "timeout_worker:DONE", secondsToWait, args


# Start up a nominal amount of worker tasklets.
for i in range(50):
    stackless.tasklet(new_tasklet)(timeout_worker, stackless.channel())


def timeout_wrap_sleep(seconds, timeoutChannel, args):
    #print "timeout_wrap_sleep:ENTER balance=%d" % timeoutChannel.balance
    if timeoutChannel.balance < 0:
        #print "timeout_wrap_sleep:SLEEP balance=%d" % timeoutChannel.balance
        sleep(seconds)
        #print "timeout_wrap_sleep:SLEPT balance=%d" % timeoutChannel.balance
        if timeoutChannel.balance < 0:
            #print "timeout_wrap_sleep:WAKEUP balance=%d" % timeoutChannel.balance
            if args is not None:
                timeoutChannel.send_exception(*args)
            else:
                timeoutChannel.send(None)
    #print "timeout_wrap_sleep:EXIT balance=%d" % timeoutChannel.balance
Пример #57
0
 def connectionMade(self):
     stackless.tasklet(self.on_message_from_chan)()
     reactor.callLater(0, stackless.schedule)
     chan_client_to_command.send("connet success")
Пример #58
0
def tasklet_new(function, args=(), kwargs={}):
    """
    A simple dispatcher which causes the function to start running on a new tasklet
    """
    return stackless.tasklet(function)(*args, **kwargs)
Пример #59
0
 def test_setstate_alive(self):
     state = stackless.tasklet().__reduce__()[2]
     t = stackless.tasklet(lambda: None,())
     self.assertTrue(t.alive)
     self.assertRaisesRegex(RuntimeError, "tasklet is alive", t.__setstate__, state)
Пример #60
0
 def lineReceived(self, line):
     stackless.tasklet(self.on_message)(line)
     reactor.callLater(0, stackless.schedule)