Skip to content
alephfirmino edited this page Jun 8, 2023 · 8 revisions

LockAPI Tutorial

Prerequisites

Before getting started, ensure that you have the following prerequisites:

Python 3.7 or higher installed on your machine. Basic knowledge of Python and asynchronous programming concepts.

Installation

pip install lockapi

This command will install the necessary packages for running the code.

Using the LockAPIClient

The LockAPIClient class allows you to create and manage locks through the Lock API.

Import the necessary classes and modules:

import httpx from aiohttp import web import nest_asyncio import asyncio

Instantiate the LockAPIClient with the desired host and port:

client = LockAPIClient(host='localhost', port=8075)

Acquire a lock:

lock_id = client.acquire_lock()

Perform your desired operations while the lock is acquired.

Release the lock:

client.release_lock(lock_id)

Close the session:

client.close_session()

Using the LockAPIServer

The LockAPIServer class provides a server for managing locks using a REST API.

Import the necessary classes and modules:

import nest_asyncio import asyncio from aiohttp import web

Instantiate the LockAPIServer:

server = LockAPIServer(port=8075)

Start the server:

await server.start()

Make requests to the server to acquire and release locks using your preferred HTTP client (e.g., httpx, requests).

Stop the server:

await server.stop()

Complete Example

Here's a complete example that demonstrates the usage of both the LockAPIClient and LockAPIServer classes:

from MyLockAPI import LockAPIClient, LockAPIServer

# Using the LockAPIServer server = LockAPIServer(port=8075) await server.start() # Make requests to acquire and release locks await server.stop()

# Using the LockAPIClient client = LockAPIClient(host='localhost', port=8075) lock_id = client.acquire_lock() # Perform operations while the lock is acquired client.release_lock(lock_id) client.close_session()

Clone this wiki locally