当前位置: 首页>>代码示例>>Python>>正文


Python asyncio.Semaphore方法代码示例

本文整理汇总了Python中asyncio.Semaphore方法的典型用法代码示例。如果您正苦于以下问题:Python asyncio.Semaphore方法的具体用法?Python asyncio.Semaphore怎么用?Python asyncio.Semaphore使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在asyncio的用法示例。


在下文中一共展示了asyncio.Semaphore方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: interval_task

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def interval_task(self):
        try:
            to_backup = self.bot.db.intervals.find({"next": {
                "$lt": datetime.utcnow()
            }})
            semaphore = Semaphore(10)
            async for interval in to_backup:
                async def run_interval():
                    try:
                        next = datetime.utcnow() + timedelta(minutes=interval["interval"])
                        await self.bot.db.intervals.update_one({"_id": interval["_id"]}, {"$set": {"next": next}})
                        await self.run_backup(interval["_id"])
                    finally:
                        semaphore.release()

                await semaphore.acquire()
                self.bot.loop.create_task(run_interval())
                await sleep(0)

        except Exception:
            pass 
开发者ID:Xenon-Bot,项目名称:xenon,代码行数:23,代码来源:backups.py

示例2: main

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def main(global_delay, local_delay, concurrency):
    global global_sleep, local_sleep, semaphore, index
    global_sleep = global_delay
    local_sleep = local_delay
    semaphore = asyncio.Semaphore(concurrency)
    print('Global delay =', global_delay)
    print('Local delay =', local_delay)
    print('Max. concurrency =', concurrency)
    print('Building inverted index...')
    index = build_index()

    app = web.Application()
    app.router.add_get('/', usage)
    app.router.add_get('/index/{word}', index_for)
    app.router.add_get('/name/{char}', char_name)

    print('Listening on port', PORT)
    web.run_app(app, port=PORT) 
开发者ID:fluentpython,项目名称:concurrency2017,代码行数:20,代码来源:signs_server.py

示例3: __init__

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def __init__(self, in_state, in_forward, search_threads):
        self.noise_eps = 0.25
        self.dirichlet_alpha = 0.3    #0.03
        self.p_ = (1 - self.noise_eps) * 1 + self.noise_eps * np.random.dirichlet([self.dirichlet_alpha])
        self.root = leaf_node(None, self.p_, in_state)
        self.c_puct = 5    #1.5
        # self.policy_network = in_policy_network
        self.forward = in_forward
        self.node_lock = defaultdict(Lock)

        self.virtual_loss = 3
        self.now_expanding = set()
        self.expanded = set()
        self.cut_off_depth = 30
        # self.QueueItem = namedtuple("QueueItem", "feature future")
        self.sem = asyncio.Semaphore(search_threads)
        self.queue = Queue(search_threads)
        self.loop = asyncio.get_event_loop()
        self.running_simulation_num = 0 
开发者ID:chengstone,项目名称:cchess-zero,代码行数:21,代码来源:main.py

示例4: _check_uris_async

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def _check_uris_async(
    links_to_check: Iterable[str], max_threads: int = 10, delay: float = 0
) -> Iterable[UrlResult]:
    tasks = []
    # create instance of Semaphore
    sem = asyncio.Semaphore(max_threads)

    # Create client session that will ensure we dont open new connection
    # per each request.
    async with ClientSession() as session:
        for uri in links_to_check:
            if delay:
                asyncio.sleep(delay)
            # pass Semaphore and session to every GET request
            task = asyncio.ensure_future(_check_uri_with_sem_async(sem, uri, session))
            tasks.append(task)

        results = await asyncio.gather(*tasks)
        return results 
开发者ID:microsoft,项目名称:msticpy,代码行数:21,代码来源:url_checker_async.py

示例5: __init__

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def __init__(self, threads: int = None) -> None:
        try:  # get or create loop (threads don't have one)
            #: our asyncio loop
            self.loop = asyncio.get_event_loop()
        except RuntimeError:
            self.loop = asyncio.new_event_loop()
            asyncio.set_event_loop(self.loop)
        #: number of threads to use
        self.threads = threads or threads_to_use()
        #: semaphore to limit io parallelism
        self.io_sem: asyncio.Semaphore = asyncio.Semaphore(1)
        #: must never run more than one conda at the same time
        #: (used by PyPi when running skeleton)
        self.conda_sem: asyncio.Semaphore = asyncio.Semaphore(1)
        #: the filters successively applied to each item
        self.filters: List[AsyncFilter] = []
        #: executor running things in separate python processes
        self.proc_pool_executor = ProcessPoolExecutor(self.threads)

        self._shutting_down = False 
开发者ID:bioconda,项目名称:bioconda-utils,代码行数:22,代码来源:aiopipe.py

示例6: __init__

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def __init__(
        self,
        context: InjectionContext,
        receive_inbound: Coroutine,
        return_inbound: Callable = None,
    ):
        """Initialize an `InboundTransportManager` instance."""
        self.context = context
        self.max_message_size = 0
        self.receive_inbound = receive_inbound
        self.return_inbound = return_inbound
        self.registered_transports = {}
        self.running_transports = {}
        self.sessions = OrderedDict()
        self.session_limit: asyncio.Semaphore = None
        self.task_queue = TaskQueue()
        self.undelivered_queue: DeliveryQueue = None 
开发者ID:hyperledger,项目名称:aries-cloudagent-python,代码行数:19,代码来源:manager.py

示例7: setup

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def setup(self):
        """Perform setup operations."""
        # Load config settings
        if self.context.settings.get("transport.max_message_size"):
            self.max_message_size = self.context.settings["transport.max_message_size"]

        inbound_transports = (
            self.context.settings.get("transport.inbound_configs") or []
        )
        for transport in inbound_transports:
            module, host, port = transport
            self.register(
                InboundTransportConfiguration(module=module, host=host, port=port)
            )

        # Setup queue for undelivered messages
        if self.context.settings.get("transport.enable_undelivered_queue"):
            self.undelivered_queue = DeliveryQueue()

        # self.session_limit = asyncio.Semaphore(50) 
开发者ID:hyperledger,项目名称:aries-cloudagent-python,代码行数:22,代码来源:manager.py

示例8: __init__

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def __init__(self, daemon, coin, blocks_event):
        self.logger = class_logger(__name__, self.__class__.__name__)
        self.daemon = daemon
        self.coin = coin
        self.blocks_event = blocks_event
        self.blocks = []
        self.caught_up = False
        # Access to fetched_height should be protected by the semaphore
        self.fetched_height = None
        self.semaphore = asyncio.Semaphore()
        self.refill_event = asyncio.Event()
        # The prefetched block cache size.  The min cache size has
        # little effect on sync time.
        self.cache_size = 0
        self.min_cache_size = 10 * 1024 * 1024
        # This makes the first fetch be 10 blocks
        self.ave_size = self.min_cache_size // 10
        self.polling_delay = 5 
开发者ID:lbryio,项目名称:torba,代码行数:20,代码来源:block_processor.py

示例9: scan_host

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def scan_host(address, start_port, end_port, max_conns=400):
        """

        :param address: IPv4 address to scan
        :param start_port: First port value to scan
        :param end_port: Last port value to scan
        :param max_conns: Maximum simultaneous number of connections
        :return:
        """
        sem = asyncio.Semaphore(max_conns)
        ports = range(start_port, end_port)
        tasks = [asyncio.ensure_future(PortScanner.check_port_sem(sem, address, port)) for port in ports]
        responses = await asyncio.gather(*tasks)
        open_ports = list(filter(lambda x: x is not None, responses))

        return open_ports 
开发者ID:aliasrobotics,项目名称:aztarna,代码行数:18,代码来源:helpers.py

示例10: find_node_ports

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def find_node_ports(address, ports):
    """
    Find all the open ports for a host.

    :param address: IP address of the host.
    :param ports: Port to check.
    :return: A list of the found open ports.
    """
    sem = asyncio.Semaphore(400)  # Change this value for concurrency limitation
    tasks = [asyncio.ensure_future(check_port_sem(sem, address, p)) for p in ports]
    found_ports = []
    for response in await asyncio.gather(*tasks):
        if response:
            found_ports.append(response)

    return found_ports 
开发者ID:aliasrobotics,项目名称:aztarna,代码行数:18,代码来源:helpers.py

示例11: check_routers

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def check_routers(self, addresses: List[str], ports: List[int]) -> List[BaseIndustrialRouter]:
        """
        Check for routers in a range of addressess and ports.

        :param addresses: List of addressess to be checked.
        :param ports: List of ports to be checked for each address.
        :return: A list of found routers.
        """
        async def check_routers_aio(addresses, ports):
            semaphore = Semaphore(50)
            futures = []
            routers = []
            for address in addresses:
                for port in ports:
                    futures.append(asyncio.ensure_future(self.check_is_router(address, port, semaphore=semaphore)))
            done, pending = await asyncio.wait(futures)
            for future in done:
                if future:
                    routers.append(future)
            return routers

        return asyncio.run(check_routers_aio(addresses, ports), debug=True) 
开发者ID:aliasrobotics,项目名称:aztarna,代码行数:24,代码来源:scanner.py

示例12: check_router_credentials

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def check_router_credentials(self, routers: List[BaseIndustrialRouter]):
        """
        Check default credentials for a list of routers.

        :param routers: List of routers to be checked.
        """
        async def check_router_credentials_aio(routers):
            semaphore = Semaphore(100)
            futures = []
            for router in routers:
                if isinstance(router, self.__class__.router_cls):
                    futures.append(asyncio.ensure_future(self.check_default_password(router, semaphore=semaphore)))

            await asyncio.wait(futures, return_when=ALL_COMPLETED, )

        asyncio.run(check_router_credentials_aio(routers), debug=True) 
开发者ID:aliasrobotics,项目名称:aztarna,代码行数:18,代码来源:scanner.py

示例13: test_context_manager_async_with

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def test_context_manager_async_with(self):
        primitives = [
            asyncio.Lock(loop=self.loop),
            asyncio.Condition(loop=self.loop),
            asyncio.Semaphore(loop=self.loop),
            asyncio.BoundedSemaphore(loop=self.loop),
        ]

        async def test(lock):
            await asyncio.sleep(0.01, loop=self.loop)
            self.assertFalse(lock.locked())
            async with lock as _lock:
                self.assertIs(_lock, None)
                self.assertTrue(lock.locked())
                await asyncio.sleep(0.01, loop=self.loop)
                self.assertTrue(lock.locked())
            self.assertFalse(lock.locked())

        for primitive in primitives:
            self.loop.run_until_complete(test(primitive))
            self.assertFalse(primitive.locked()) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_pep492.py

示例14: test_context_manager_with_await

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def test_context_manager_with_await(self):
        primitives = [
            asyncio.Lock(loop=self.loop),
            asyncio.Condition(loop=self.loop),
            asyncio.Semaphore(loop=self.loop),
            asyncio.BoundedSemaphore(loop=self.loop),
        ]

        async def test(lock):
            await asyncio.sleep(0.01, loop=self.loop)
            self.assertFalse(lock.locked())
            with await lock as _lock:
                self.assertIs(_lock, None)
                self.assertTrue(lock.locked())
                await asyncio.sleep(0.01, loop=self.loop)
                self.assertTrue(lock.locked())
            self.assertFalse(lock.locked())

        for primitive in primitives:
            self.loop.run_until_complete(test(primitive))
            self.assertFalse(primitive.locked()) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_pep492.py

示例15: test_semaphore

# 需要导入模块: import asyncio [as 别名]
# 或者: from asyncio import Semaphore [as 别名]
def test_semaphore(self):
        sem = asyncio.Semaphore(loop=self.loop)
        self.assertEqual(1, sem._value)

        @asyncio.coroutine
        def acquire_lock():
            return (yield from sem)

        res = self.loop.run_until_complete(acquire_lock())

        self.assertTrue(res)
        self.assertTrue(sem.locked())
        self.assertEqual(0, sem._value)

        sem.release()
        self.assertFalse(sem.locked())
        self.assertEqual(1, sem._value) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:19,代码来源:test_locks.py


注:本文中的asyncio.Semaphore方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。