本文整理匯總了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
示例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)
示例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
示例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
示例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
示例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
示例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)
示例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
示例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
示例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
示例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)
示例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)
示例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())
示例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())
示例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)