Python trio.Lock() Examples
The following are 18
code examples of trio.Lock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
trio
, or try the search function
.
Example #1
Source File: endpoint.py From lahja with MIT License | 6 votes |
def __init__( self, local_name: str, conn: ConnectionAPI, subscriptions_changed: ConditionAPI, new_msg_func: Callable[[Broadcast], Awaitable[Any]], ) -> None: super().__init__(local_name, conn, new_msg_func) self._notify_lock = trio.Lock() # type: ignore self._received_response = trio.Condition() # type: ignore self._received_subscription = trio.Condition() # type: ignore self._running = trio.Event() # type: ignore self._stopped = trio.Event() # type: ignore self._received_subscription = subscriptions_changed self._subscriptions_initialized = trio.Event() # type: ignore self._running = trio.Event() # type: ignore self._stopped = trio.Event() # type: ignore self._ready = trio.Event() # type: ignore
Example #2
Source File: net_trio.py From rethinkdb-python with Apache License 2.0 | 5 votes |
def __init__(self, parent, nursery=None): self._stream = None self._stream_lock = trio.Lock() self._sockname = None self._parent = parent self._closing = False self._closed = False self._user_queries = {} self._cursor_cache = {} self._reader_ended_event = None self._nursery = nursery
Example #3
Source File: beacon_node.py From trinity with MIT License | 5 votes |
def __init__( self, genesis_time: int, beacon_node_endpoint: str, seconds_per_slot: int ) -> None: self._genesis_time = genesis_time self._beacon_node_endpoint = _normalize_url(beacon_node_endpoint) self._seconds_per_slot = seconds_per_slot self._ticks_per_slot = TICKS_PER_SLOT self._session = Session() self._connection_lock = trio.Lock() self._is_connected = False self.client_version: Optional[str] = None # NOTE: this facilitates testing, may remove in the future... self._broadcast_operations: Set[Root] = set()
Example #4
Source File: duty_store.py From trinity with MIT License | 5 votes |
def __init__(self) -> None: self._store: Dict[TickCount, Tuple[Duty, ...]] = defaultdict(tuple) self._store_lock = trio.Lock()
Example #5
Source File: packer.py From trinity with MIT License | 5 votes |
def __init__(self, local_private_key: bytes, local_node_id: NodeID, remote_node_id: NodeID, node_db: NodeDBAPI, message_type_registry: MessageTypeRegistry, incoming_packet_receive_channel: ReceiveChannel[IncomingPacket], incoming_message_send_channel: SendChannel[IncomingMessage], outgoing_message_receive_channel: ReceiveChannel[OutgoingMessage], outgoing_packet_send_channel: SendChannel[OutgoingPacket], ) -> None: self.local_private_key = local_private_key self.local_node_id = local_node_id self.remote_node_id = remote_node_id self.node_db = node_db self.message_type_registry = message_type_registry self.incoming_packet_receive_channel = incoming_packet_receive_channel self.incoming_message_send_channel = incoming_message_send_channel self.outgoing_message_receive_channel = outgoing_message_receive_channel self.outgoing_packet_send_channel = outgoing_packet_send_channel self.logger = logging.getLogger( f"p2p.discv5.packer.PeerPacker[{encode_hex(remote_node_id)[2:10]}]" ) self.outgoing_message_backlog: List[OutgoingMessage] = [] # This lock ensures that at all times, at most one incoming packet or outgoing message is # being processed. self.handling_lock = trio.Lock() self.handshake_successful_event: Optional[trio.Event] = None
Example #6
Source File: endpoint.py From lahja with MIT License | 5 votes |
def __init__(self, name: str): super().__init__(name) self._running = trio.Event() self._stopped = trio.Event() # Temporary storage for SendChannels used in the `stream` API. Uses a # `WeakSet` to automate cleanup. self._stream_channels = collections.defaultdict(set) self._pending_requests = {} self._sync_handlers = collections.defaultdict(list) self._async_handlers = collections.defaultdict(list) self._run_lock = trio.Lock() # Signal when a new remote connection is established self._remote_connections_changed = trio.Condition() # type: ignore # Signal when at least one remote has had a subscription change. self._remote_subscriptions_changed = trio.Condition() # type: ignore # events used to signal that the endpoint has fully booted up. self._message_processing_loop_running = trio.Event() self._connection_loop_running = trio.Event() self._process_broadcasts_running = trio.Event() # internal signal that local subscriptions have changed. self._subscriptions_changed = trio.Event() self._socket_bound = trio.Event() self._server_stopped = trio.Event() # # Running and endpoint #
Example #7
Source File: endpoint.py From lahja with MIT License | 5 votes |
def __init__(self, socket: trio.SocketStream) -> None: self._socket = socket self._msg_send_channel, self._msg_receive_channel = cast( Tuple[trio.abc.SendChannel[Message], trio.abc.ReceiveChannel[Message]], trio.open_memory_channel(100), ) self._write_lock = trio.Lock() super().__init__()
Example #8
Source File: conftest.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def _test_target(self, fn, **kwargs): # Initialize trio objects self._lock = trio.Lock() self._stopping = trio.Event() self._trio_token = trio.hazmat.current_trio_token() # Set the started state event self._started.set() # Run the test try: result = await self._run_with_job_scheduler(fn, **kwargs) except BaseException as exc: self._test_result.set_exception(exc) else: self._test_result.set_result(result) # Indicate there will be no more requests self._request_queue.put_nowait(None) # Let the trio loop run until teardown await self._stopping.wait() # Not an async fixture (and doesn't depend on an async fixture either) # this fixture will actually be executed *before* pytest-trio setup # the trio loop, giving us a chance to monkeypatch it !
Example #9
Source File: workspace_storage.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def __init__( self, device: LocalDevice, path: Path, workspace_id: EntryID, data_localdb: LocalDatabase, cache_localdb: LocalDatabase, block_storage: ChunkStorage, chunk_storage: ChunkStorage, manifest_storage: ManifestStorage, ): self.path = path self.device = device self.device_id = device.device_id self.workspace_id = workspace_id # File descriptors self.open_fds: Dict[FileDescriptor, EntryID] = {} self.fd_counter = 0 # Locking structures self.locking_tasks = {} self.entry_locks = defaultdict(trio.Lock) # Manifest and block storage self.data_localdb = data_localdb self.cache_localdb = cache_localdb self.manifest_storage = manifest_storage self.block_storage = block_storage self.chunk_storage = chunk_storage
Example #10
Source File: local_database.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, path, vacuum_threshold=None): self._conn = None self._lock = trio.Lock() self._run_in_thread = None self.path = Path(path) self.vacuum_threshold = vacuum_threshold
Example #11
Source File: workspacefs.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def __init__( self, workspace_id: EntryID, get_workspace_entry, device: LocalDevice, local_storage, backend_cmds, event_bus, remote_device_manager, ): self.workspace_id = workspace_id self.get_workspace_entry = get_workspace_entry self.device = device self.local_storage = local_storage self.backend_cmds = backend_cmds self.event_bus = event_bus self.remote_device_manager = remote_device_manager self.sync_locks = defaultdict(trio.Lock) self.remote_loader = RemoteLoader( self.device, self.workspace_id, self.get_workspace_entry, self.backend_cmds, self.remote_device_manager, self.local_storage, ) self.transactions = SyncTransactions( self.workspace_id, self.get_workspace_entry, self.device, self.local_storage, self.remote_loader, self.event_bus, )
Example #12
Source File: tcp_server.py From hypercorn with MIT License | 5 votes |
def __init__(self, app: ASGIFramework, config: Config, stream: trio.abc.Stream) -> None: self.app = app self.config = config self.protocol: ProtocolWrapper self.send_lock = trio.Lock() self.timeout_lock = trio.Lock() self.stream = stream self._keep_alive_timeout_handle: Optional[trio.CancelScope] = None
Example #13
Source File: apiv1_annonymous.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def apiv1_backend_anonymous_cmds_factory( addr: BackendOrganizationAddr, keepalive: Optional[int] = None ) -> AsyncGenerator[APIV1_BackendAnonymousCmds, None]: """ Raises: BackendConnectionError """ transport_lock = trio.Lock() transport = None closed = False async def _init_transport(): nonlocal transport if not transport: if closed: raise trio.ClosedResourceError transport = await apiv1_connect(addr, keepalive=keepalive) transport.logger = transport.logger.bind(auth="<anonymous>") async def _destroy_transport(): nonlocal transport if transport: await transport.aclose() transport = None @asynccontextmanager async def _acquire_transport(**kwargs): nonlocal transport async with transport_lock: await _init_transport() try: yield transport except BackendNotAvailable: await _destroy_transport() raise try: yield APIV1_BackendAnonymousCmds(addr, _acquire_transport) finally: async with transport_lock: closed = True await _destroy_transport()
Example #14
Source File: apiv1_authenticated.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def apiv1_backend_authenticated_cmds_factory( addr: BackendOrganizationAddr, device_id: DeviceID, signing_key: SigningKey, keepalive: Optional[int] = None, ) -> AsyncGenerator[APIV1_BackendAuthenticatedCmds, None]: """ Raises: BackendConnectionError """ transport_lock = trio.Lock() transport = None closed = False async def _init_transport(): nonlocal transport if not transport: if closed: raise trio.ClosedResourceError transport = await apiv1_connect( addr, device_id=device_id, signing_key=signing_key, keepalive=keepalive ) transport.logger = transport.logger.bind(device_id=device_id) async def _destroy_transport(): nonlocal transport if transport: await transport.aclose() transport = None @asynccontextmanager async def _acquire_transport(**kwargs): nonlocal transport async with transport_lock: await _init_transport() try: yield transport except BackendNotAvailable: await _destroy_transport() raise try: yield APIV1_BackendAuthenticatedCmds(addr, _acquire_transport) finally: async with transport_lock: closed = True await _destroy_transport()
Example #15
Source File: authenticated.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def backend_authenticated_cmds_factory( addr: BackendOrganizationAddr, device_id: DeviceID, signing_key: SigningKey, keepalive: Optional[int] = None, ) -> AsyncGenerator[BackendAuthenticatedCmds, None]: """ Raises: BackendConnectionError """ transport_lock = trio.Lock() transport = None closed = False async def _init_transport(): nonlocal transport if not transport: if closed: raise trio.ClosedResourceError transport = await connect_as_authenticated( addr, device_id=device_id, signing_key=signing_key, keepalive=keepalive ) transport.logger = transport.logger.bind(device_id=device_id) async def _destroy_transport(): nonlocal transport if transport: await transport.aclose() transport = None @asynccontextmanager async def _acquire_transport(**kwargs): nonlocal transport async with transport_lock: await _init_transport() try: yield transport except BackendNotAvailable: await _destroy_transport() raise try: yield BackendAuthenticatedCmds(addr, _acquire_transport) finally: async with transport_lock: closed = True await _destroy_transport()
Example #16
Source File: invited.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def backend_invited_cmds_factory( addr: BackendInvitationAddr, keepalive: Optional[int] = None ) -> AsyncGenerator[BackendInvitedCmds, None]: """ Raises: BackendConnectionError """ transport_lock = trio.Lock() transport = None closed = False async def _init_transport(): nonlocal transport if not transport: if closed: raise trio.ClosedResourceError transport = await connect_as_invited(addr, keepalive=keepalive) transport.logger = transport.logger.bind(auth="<invited>") async def _destroy_transport(): nonlocal transport if transport: await transport.aclose() transport = None @asynccontextmanager async def _acquire_transport(**kwargs): nonlocal transport async with transport_lock: await _init_transport() try: yield transport except BackendNotAvailable: await _destroy_transport() raise try: yield BackendInvitedCmds(addr, _acquire_transport) finally: async with transport_lock: closed = True await _destroy_transport()
Example #17
Source File: discovery.py From trinity with MIT License | 4 votes |
def __init__(self, privkey: datatypes.PrivateKey, udp_port: int, tcp_port: int, bootstrap_nodes: Sequence[NodeAPI], event_bus: EndpointAPI, socket: trio.socket.SocketType, node_db: NodeDBAPI, enr_field_providers: Sequence[ENR_FieldProvider] = tuple(), ) -> None: self.logger = get_logger('p2p.discovery.DiscoveryService') self.privkey = privkey self._event_bus = event_bus self.enr_response_channels = ExpectedResponseChannels[Tuple[ENR, Hash32]]() self.pong_channels = ExpectedResponseChannels[Tuple[Hash32, int]]() self.neighbours_channels = ExpectedResponseChannels[List[NodeAPI]]() self.ping_channels = ExpectedResponseChannels[None]() self.enr_field_providers = enr_field_providers self.node_db = node_db self._local_enr_next_refresh: float = time.monotonic() self._local_enr_lock = trio.Lock() self._lookup_lock = trio.Lock() self.parity_pong_tokens: Dict[Hash32, Hash32] = {} if socket.family != trio.socket.AF_INET: raise ValueError("Invalid socket family") elif socket.type != trio.socket.SOCK_DGRAM: raise ValueError("Invalid socket type") self.socket = socket self.pending_enrs_producer, self.pending_enrs_consumer = trio.open_memory_channel[ Tuple[NodeID, int]](self._max_pending_enrs) # Ensure our bootstrap nodes are in our DB and keep only a reference to their IDs. This is # to ensure self.bootstrap_nodes always returns the last known version of them. self._bootstrap_node_ids: List[NodeID] = [] for node in bootstrap_nodes: self._bootstrap_node_ids.append(node.id) try: self.node_db.get_enr(node.id) except KeyError: self.node_db.set_enr(node.enr) if len(set(self._bootstrap_node_ids)) != len(self._bootstrap_node_ids): raise ValueError( "Multiple bootnodes with the same ID are not allowed: {self._bootstrap_node_ids}") # This is a stub Node instance (which will have an ENR with sequence number 0) that will # be replaced in _init(). We could defer its creation until _init() is called by run(), # but doing this here simplifies things as we don't have to store the Address in another # instance attribute. self.this_node: NodeAPI = Node.from_pubkey_and_addr( self.pubkey, Address("127.0.0.1", udp_port, tcp_port), ) self.routing = KademliaRoutingTable(self.this_node.id, constants.KADEMLIA_BUCKET_SIZE)
Example #18
Source File: userfs.py From parsec-cloud with GNU Affero General Public License v3.0 | 4 votes |
def __init__( self, device: LocalDevice, path: Path, backend_cmds: APIV1_BackendAuthenticatedCmds, remote_devices_manager: RemoteDevicesManager, event_bus: EventBus, ): self.device = device self.path = path self.backend_cmds = backend_cmds self.remote_devices_manager = remote_devices_manager self.event_bus = event_bus self.storage = None # Message processing is done in-order, hence it is pointless to do # it concurrently self._workspace_storage_nursery = None self._process_messages_lock = trio.Lock() self._update_user_manifest_lock = trio.Lock() self._workspace_storages = {} now = pendulum_now() wentry = WorkspaceEntry( name="<user manifest>", id=device.user_manifest_id, key=device.user_manifest_key, encryption_revision=1, encrypted_on=now, role_cached_on=now, role=WorkspaceRole.OWNER, ) self.remote_loader = RemoteLoader( self.device, self.device.user_manifest_id, lambda: wentry, self.backend_cmds, self.remote_devices_manager, # Hack, but fine as long as we only call `load_realm_current_roles` None, )