コード例 #1
0
ファイル: amqp.py プロジェクト: mkushnir/amqp-shrapnel
 def __init__ (self, auth, host, port=5672, virtual_host='/', heartbeat=0, consumer_cancel_notify=None, frame_max=0):
     self.port = port
     self.host = host
     self.auth = auth
     self.virtual_host = virtual_host
     self.heartbeat = heartbeat
     self.frame_state = 0
     self.frames = coro.fifo()
     # collect body parts here.  heh.
     self.body = []
     self.next_content_consumer = None
     self.next_properties = None
     self.consumers = {}
     self.last_sent_before = coro.tsc_time.now_raw_posix_fsec()
     self.last_sent_after = coro.tsc_time.now_raw_posix_fsec()
     self.channels = {}
     self._exception_handler = None
     self._send_completion_handler = None
     self._recv_completion_handler = None
     self.consumer_cancel_notify = consumer_cancel_notify
     self.frame_max = frame_max
     # XXX implement read/write "channels" (coro).
     self._recv_loop_thread = None
     self._s_recv_sema = coro.semaphore(1)
     self._s_send_sema = coro.semaphore(1)
コード例 #2
0
ファイル: server.py プロジェクト: likecoin-dev/caesure
def go(args, global_state):
    global G
    G = global_state
    G.args = args
    G.logger = ASN1_Logger(open(os.path.join(G.args.base, 'log.asn1'), 'ab'))
    G.log = G.logger.log
    # needed for the sub-imports below...
    import coro
    coro.set_exception_notifier(exception_notifier)
    G.log('starting caesure')
    G.addr_cache = AddressCache()
    G.block_db = block_db.BlockDB(read_only=False)
    G.hoover = BlockHoover()
    G.txn_pool = TransactionPool()
    G.recent_blocks = ledger.catch_up(G)
    G.verbose = args.verbose
    G.connection_map = {}
    # install a real resolver
    coro.dns.cache.install()
    if args.monitor:
        import coro.backdoor
        coro.spawn(coro.backdoor.serve, unix_path='/tmp/caesure.bd')
    users = {}
    if args.user:
        for user in args.user:
            u, p = user.split(':')
            users[u] = p
    if args.webui:
        import coro.http
        import caesure.webadmin
        import zlib
        G.http_server = h = coro.http.server()
        G.webadmin_handler = caesure.webadmin.handler(G)
        if users:
            h.push_handler(
                coro.http.handlers.auth_handler(users, G.webadmin_handler))
            coro.spawn(h.start, (('', 8380)))
        else:
            h.push_handler(G.webadmin_handler)
            coro.spawn(h.start, (('127.0.0.1', 8380)))
        h.push_handler(coro.http.handlers.coro_status_handler())
        h.push_handler(
            coro.http.handlers.favicon_handler(
                zlib.compress(caesure.webadmin.favicon)))
    G.in_conn_sem = coro.semaphore(args.incoming)
    G.out_conn_sem = coro.semaphore(args.outgoing)
    if args.relay:
        Connection.relay = True
    if args.serve:
        for addr in args.serve:
            coro.spawn(serve, addr)
    if args.connect:
        for addr in args.connect:
            coro.spawn(connect, addr)
    coro.spawn(G.addr_cache.purge_thread)
    coro.spawn(new_block_thread)
    coro.spawn(new_connection_thread)
    coro.spawn(G.recent_blocks.save_ledger_thread)
コード例 #3
0
ファイル: test_semaphore.py プロジェクト: halayli/shrapnel
    def test_exception_stomp(self):
        # Pyrex had a bug where if it raised an exception it would stomp on
        # the "current" exception on the Python stack.
        s = coro.semaphore(0)

        def blocker():
            s.acquire(1)

        t1 = coro.spawn(blocker)
        coro.yield_slice()
        # Mark the thread as scheduled.
        t1.shutdown()

        def raiser():
            try:
                raise ValueError(3)
            except ValueError:
                # This will attempt to schedule t1 which will result in a
                # ScheduleError getting raised and caught within the release
                # code.
                s.release(1)
                # This should re-raise ValueError.  But with the bug, it was
                # re-raising ScheduleError.
                raise

        self.assertRaises(ValueError, raiser)
コード例 #4
0
    def test_exception_stomp(self):
        # Pyrex had a bug where if it raised an exception it would stomp on
        # the "current" exception on the Python stack.
        s = coro.semaphore(0)

        def blocker():
            s.acquire(1)

        t1 = coro.spawn(blocker)
        coro.yield_slice()
        # Mark the thread as scheduled.
        t1.shutdown()

        def raiser():
            try:
                raise ValueError(3)
            except ValueError:
                # This will attempt to schedule t1 which will result in a
                # ScheduleError getting raised and caught within the release
                # code.
                s.release(1)
                # This should re-raise ValueError.  But with the bug, it was
                # re-raising ScheduleError.
                raise

        self.assertRaises(ValueError, raiser)
コード例 #5
0
ファイル: server.py プロジェクト: githubhjs/caesure
def main1 (args, G):
    G.verbose = args.verbose
    G.addr_cache = AddressCache()
    G.block_db = block_db.BlockDB (read_only=False)
    G.hoover = BlockHoover()
    G.txn_pool = TransactionPool()
    G.recent_blocks = ledger.catch_up (G)
    G.connection_map = {}
    # install a real resolver
    coro.dns.cache.install()
    if args.monitor:
        coro.spawn (coro.backdoor.serve, unix_path=network.BD_FILE)
    users = {}
    if args.user:
        for user in args.user:
            u, p = user.split (':')
            users[u] = p
    if args.webui:
        G.http_server = h = coro.http.server()
        G.webadmin_handler = caesure.webadmin.handler (G)
        if users:
            h.push_handler (coro.http.handlers.auth_handler (users, G.webadmin_handler))
            coro.spawn (h.start, (('', network.ADMIN_PORT)))
        else:
            h.push_handler (G.webadmin_handler)
            coro.spawn (h.start, (('127.0.0.1', network.ADMIN_PORT)))
        h.push_handler (coro.http.handlers.coro_status_handler())
        h.push_handler (
            coro.http.handlers.favicon_handler (
                zlib.compress (caesure.webadmin.favicon)
            )
        )
    G.in_conn_sem = coro.semaphore (args.incoming)
    G.out_conn_sem = coro.semaphore (args.outgoing)
    if args.relay:
        Connection.relay = True
    if args.serve:
        for addr in args.serve:
            coro.spawn (serve, addr)
    if args.connect:
        for addr in args.connect:
            coro.spawn (connect, addr)
    coro.spawn (G.addr_cache.purge_thread)
    coro.spawn (new_block_thread)
    coro.spawn (new_connection_thread)
    coro.spawn (G.recent_blocks.save_ledger_thread)
コード例 #6
0
def main1 (args, G):
    G.verbose = args.verbose
    G.addr_cache = AddressCache()
    G.block_db = block_db.BlockDB (read_only=False)
    G.hoover = BlockHoover()
    G.txn_pool = TransactionPool()
    G.recent_blocks = ledger.catch_up (G)
    G.connection_map = {}
    # install a real resolver
    coro.dns.cache.install()
    if args.monitor:
        coro.spawn (coro.backdoor.serve, unix_path=network.BD_FILE)
    users = {}
    if args.user:
        for user in args.user:
            u, p = user.split (':')
            users[u] = p
    if args.webui:
        G.http_server = h = coro.http.server()
        G.webadmin_handler = caesure.webadmin.handler (G)
        if users:
            h.push_handler (coro.http.handlers.auth_handler (users, G.webadmin_handler))
            coro.spawn (h.start, (('', network.ADMIN_PORT)))
        else:
            h.push_handler (G.webadmin_handler)
            coro.spawn (h.start, (('127.0.0.1', network.ADMIN_PORT)))
        h.push_handler (coro.http.handlers.coro_status_handler())
        h.push_handler (
            coro.http.handlers.favicon_handler (
                zlib.compress (caesure.webadmin.favicon)
            )
        )
    G.in_conn_sem = coro.semaphore (args.incoming)
    G.out_conn_sem = coro.semaphore (args.outgoing)
    if args.relay:
        Connection.relay = True
    if args.serve:
        for addr in args.serve:
            coro.spawn (serve, addr)
    if args.connect:
        for addr in args.connect:
            coro.spawn (connect, addr)
    coro.spawn (G.addr_cache.purge_thread)
    coro.spawn (new_block_thread)
    coro.spawn (new_connection_thread)
    coro.spawn (G.recent_blocks.save_ledger_thread)
コード例 #7
0
ファイル: server.py プロジェクト: likecoin-dev/caesure
 def __init__(self, in_flight=20):
     self.queue = coro.fifo()
     self.qset = set()
     self.requested = set()
     self.ready = {}
     self.target = G.block_db.last_block
     self.running = False
     self.live_cv = coro.condition_variable()
     self.in_flight_sem = coro.semaphore(in_flight)
コード例 #8
0
ファイル: server.py プロジェクト: npc7/caesure
 def __init__ (self, in_flight=20):
     self.queue = coro.fifo()
     self.qset = set()
     self.requested = set()
     self.ready = {}
     self.target = G.block_db.last_block
     self.running = False
     self.live_cv = coro.condition_variable()
     self.in_flight_sem = coro.semaphore (in_flight)
コード例 #9
0
ファイル: spdy.py プロジェクト: markpeek/shrapnel
 def run(self):
     self.streams = {}
     self.deflate = deflator()
     self.inflate = inflator()
     self.ofifo = coro.fifo()
     self.obuf = coro.semaphore(self.output_buffer_size)
     coro.spawn(self.send_thread)
     try:
         self.read_frames()
     finally:
         self.ofifo.push(None)
コード例 #10
0
ファイル: client.py プロジェクト: vijaysharma136/Hello-world
 def __init__ (self, host, port=80, conn=None, inflight=100):
     self.host = host
     self.inflight = coro.semaphore (inflight)
     if conn is None:
         self.conn = coro.tcp_sock()
         self.conn.connect ((host, port))
     else:
         self.conn = conn
     self.stream = coro.read_stream.sock_stream (self.conn)
     self.pending = coro.fifo()
     coro.spawn (self.read_thread)
コード例 #11
0
ファイル: spdy.py プロジェクト: pandyxu/shrapnel
 def run(self):
     self.streams = {}
     self.deflate = deflator()
     self.inflate = inflator()
     self.ofifo = coro.fifo()
     self.obuf = coro.semaphore(self.output_buffer_size)
     coro.spawn(self.send_thread)
     try:
         self.read_frames()
     finally:
         self.ofifo.push(None)
コード例 #12
0
 def test_exception_stomp2(self):
     # Pyrex had a bug where calling a function within an exception handler,
     # and that function raised and caught an exception, it would stomp on
     # the current exception, so re-raising would raise the wrong exception.
     s = coro.semaphore(0)
     def blocker():
         s.acquire(1)
     t1 = coro.spawn(blocker)
     t2 = coro.spawn(blocker)
     coro.yield_slice()
     # Make t1 scheduled.
     s.release(1)
     # Interrupt t1, it will try to schedule t2, but that will fail.
     t1.shutdown()
     # Cause t2 to be scheduled.
     t2.shutdown()
     coro.yield_slice()
コード例 #13
0
ファイル: test_semaphore.py プロジェクト: amitdev/shrapnel
    def test_exception_stomp2(self):
        # Pyrex had a bug where calling a function within an exception handler,
        # and that function raised and caught an exception, it would stomp on
        # the current exception, so re-raising would raise the wrong exception.
        s = coro.semaphore(0)

        def blocker():
            s.acquire(1)
        t1 = coro.spawn(blocker)
        t2 = coro.spawn(blocker)
        coro.yield_slice()
        # Make t1 scheduled.
        s.release(1)
        # Interrupt t1, it will try to schedule t2, but that will fail.
        t1.shutdown()
        # Cause t2 to be scheduled.
        t2.shutdown()
        coro.yield_slice()
コード例 #14
0
 def test_sem_schedule_interrupt(self):
     """Test schedule then interrupt on semaphore."""
     s = coro.semaphore(5)
     s.acquire(5)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._sem_block, s)
     coro.yield_slice()
     # Schedule all of the threads (except the no interrupt thread).
     s.release(5)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
コード例 #15
0
 def test_sem_interrupt_schedule(self):
     """Test interrupt then schedule on semaphore."""
     s = coro.semaphore(1)
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._sem_block, s)
     coro.yield_slice()
     # Cause an interrupt on these threads.
     for t in threads:
         t.shutdown()
     # Now try to get the non-interrupted thread to run.
     s.release(1)
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
コード例 #16
0
ファイル: test_semaphore.py プロジェクト: amitdev/shrapnel
 def test_sem_schedule_interrupt(self):
     """Test schedule then interrupt on semaphore."""
     s = coro.semaphore(5)
     s.acquire(5)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._sem_block, s)
     coro.yield_slice()
     # Schedule all of the threads (except the no interrupt thread).
     s.release(5)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
コード例 #17
0
ファイル: test_semaphore.py プロジェクト: amitdev/shrapnel
 def test_sem_interrupt_schedule(self):
     """Test interrupt then schedule on semaphore."""
     s = coro.semaphore(1)
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._sem_block, s)
     coro.yield_slice()
     # Cause an interrupt on these threads.
     for t in threads:
         t.shutdown()
     # Now try to get the non-interrupted thread to run.
     s.release(1)
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
コード例 #18
0
ファイル: test_semaphore.py プロジェクト: amitdev/shrapnel
 def test_sem_buildup(self):
     """Test semaphore waiting buildup."""
     # There was a bad bug once where the _waiting list got really big,
     # and a lot of interrupts was causing a lot of thrashing of the
     # _waiting list.
     s = coro.semaphore(1)
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     coro.yield_slice()
     self.assertEqual(len(s._waiting), 5)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     self.assertEqual(len(s._waiting), 5)
     # Now try to release.
     s.release(1)
     self.assertEqual(len(s._waiting), 0)
     coro.yield_slice()
     self.assertEqual(self._resume_count, 0)
コード例 #19
0
 def test_sem_buildup(self):
     """Test semaphore waiting buildup."""
     # There was a bad bug once where the _waiting list got really big,
     # and a lot of interrupts was causing a lot of thrashing of the
     # _waiting list.
     s = coro.semaphore(1)
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._sem_block, s))
     coro.yield_slice()
     self.assertEqual(len(s._waiting), 5)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     self.assertEqual(len(s._waiting), 5)
     # Now try to release.
     s.release(1)
     self.assertEqual(len(s._waiting), 0)
     coro.yield_slice()
     self.assertEqual(self._resume_count, 0)
コード例 #20
0
ファイル: stub_resolver.py プロジェクト: AdrianScott/shrapnel
 def __init__ (self, nameservers, inflight=200):
     self.nameservers = nameservers
     self.inflight = coro.semaphore (inflight)
     self.inflight_ids = set()
コード例 #21
0
ファイル: threading.py プロジェクト: tdraegen/shrapnel
 def __init__(self, value=1):
     self._sem = coro.semaphore(value)
コード例 #22
0
ファイル: threading.py プロジェクト: codemac/shrapnel
 def __init__(self, value=1):
     self._sem = coro.semaphore(value)
コード例 #23
0
ファイル: server.py プロジェクト: npc7/caesure
def go (args, global_state):
    global G
    G = global_state
    G.args = args
    G.logger = ASN1_Logger (
        open (os.path.join (G.args.base, 'log.asn1'), 'ab')
        )
    G.log = G.logger.log
    # needed for the sub-imports below...
    import coro
    coro.set_exception_notifier (exception_notifier)
    G.log ('starting caesure')
    G.addr_cache = AddressCache()
    G.block_db = block_db.BlockDB (read_only=False)
    G.hoover = BlockHoover()
    G.txn_pool = TransactionPool()
    G.recent_blocks = ledger.catch_up (G)
    G.verbose = args.verbose
    G.connection_map = {}
    # install a real resolver
    coro.dns.cache.install()
    if args.monitor:
        import coro.backdoor
        coro.spawn (coro.backdoor.serve, unix_path='/tmp/caesure.bd')
    users = {}
    if args.user:
        for user in args.user:
            u, p = user.split (':')
            users[u] = p
    if args.webui:
        import coro.http
        import caesure.webadmin
        import zlib
        G.http_server = h = coro.http.server()
        G.webadmin_handler = caesure.webadmin.handler (G)
        if users:
            h.push_handler (coro.http.handlers.auth_handler (users, G.webadmin_handler))
            coro.spawn (h.start, (('', 8380)))
        else:
            h.push_handler (G.webadmin_handler)
            coro.spawn (h.start, (('127.0.0.1', 8380)))
        h.push_handler (coro.http.handlers.coro_status_handler())
        h.push_handler (
            coro.http.handlers.favicon_handler (
                zlib.compress (caesure.webadmin.favicon)
            )
        )
    G.in_conn_sem = coro.semaphore (args.incoming)
    G.out_conn_sem = coro.semaphore (args.outgoing)
    if args.relay:
        Connection.relay = True
    if args.serve:
        for addr in args.serve:
            coro.spawn (serve, addr)
    if args.connect:
        for addr in args.connect:
            coro.spawn (connect, addr)
    coro.spawn (G.addr_cache.purge_thread)
    coro.spawn (new_block_thread)
    coro.spawn (new_connection_thread)
    coro.spawn (G.recent_blocks.save_ledger_thread)
コード例 #24
0
 def __init__(self, nameservers, inflight=200):
     self.nameservers = nameservers
     self.inflight = coro.semaphore(inflight)
     self.inflight_ids = set()
コード例 #25
0
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

oranges_left = coro.semaphore(10)

def func1():
    print "func1 getting some oranges."
    oranges_left.acquire(9)
    print "func1 done"

def func2():
    print "func2 getting some oranges."
    oranges_left.acquire(3)
    print "func2 done"
    coro._exit = 1

def func3():
    print "func3 releasing oranges from func1"
    oranges_left.release(9)
コード例 #26
0
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

oranges_left = coro.semaphore(10)


def func1():
    print "func1 getting some oranges."
    oranges_left.acquire(9)
    print "func1 done"


def func2():
    print "func2 getting some oranges."
    oranges_left.acquire(3)
    print "func2 done"
    coro._exit = 1