Package: comlink
Send and receive messages by using SQS queues.pipinstallcomlinkpoetryaddcomlinkExamplefromcomlinkimportSqsConsumer,SqsQueue,signal_eventasyncdefexample(queue_url,sqs_client):# Create a queue objectsqs_queue=SqsQueue(url=queue_url,client=sqs_client)# Event for stopping the consumer by receiving os signal.# stop_event waits for SIGINT(Ctrl+C) or SIGTERM by defaultstop_event=signal_event()# Create a consumer with a handler that just prints the messageconsumer=SqsConsumer(queue=sqs_queue,handler=print)# Start the consumerconsumer_task=awaitconsumer.start(stop_event=stop_event)# Send a message to the queueawaitsqs_queue.put("Hello, world!")# Wait for the consumer to stop by receiving os signalawaitconsumer_taskMore examples can be found in theexamplesdirectory.DocsSqsQueuecoroutine put(data, **kwargs)coroutine take(max_messages, visibility_timeout, wait_time_seconds, **kwargs)remove(receipt_handle, **kwargs)SqsConsumercoroutine start(stop_event)signal_eventcomlink.SqsQueue(url, client, serializer=None, deserializer=None)It is a wrapper around aiobotocore's SQS client. It provides a simple interface for sending and receiving messages.In most cases, you will need to pass a instance of this class to thecomlink.SqsConsumerand send messages to it using theputmethod.takeandremovemethods are used by theSqsConsumerclass. But you can use them if you need to.Parameters:url: str- URL of the queueclient: Any- aiobotocore's SQS clientserializer: Callable[[Message], str] | None- a function that serializes the message to stringdeserializer: Callable[[str], Message] | None- a function that deserializes the message from stringExampleimportjsonfromcomlinkimportSqsQueueasyncdefexample(queue_url,sqs_client):sqs_queue=SqsQueue(url=queue_url,client=sqs_client,serializer=json.dumps,deserializer=json.loads)awaitsqs_queue.put({"hello":"world"})message=awaitsqs_queue.take(max_messages=1,visibility_timeout=10,wait_time_seconds=0)print(message[0]["Body"])awaitsqs_queue.remove(message[0]["ReceiptHandle"])coroutineput(data, **kwargs)Sends a message to the queue.Parameters:data: Message- message data. Ifserializeris provided, it will be called withdataas an argument.kwargs- additional arguments for aiobotocore'ssend_messagemethod. E.g.DelaySeconds,MessageGroupId,MessageDeduplicationIdand etc.Returns result of aiobotocore'ssend_messagemethod.coroutinetake(max_messages, visibility_timeout, wait_time_seconds, **kwargs)Get messages from the queue.Parameters:max_messages: int- maximum number of messages to receivevisibility_timeout: int- the duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by atakerequestwait_time_seconds: int- the duration (in seconds) for which the call waits for a message to arrive in the queue before returning. If a message is available, the call returns sooner thanwait_time_seconds. If no messages are available and the wait time expires, the call returns successfully with an empty list of messages.kwargs- additional arguments for aiobotocore'sreceive_messagemethod. E.g.AttributeNames,MessageAttributeNamesand etc.Returns list of messages that were received from the queue.coroutineremove(receipt_handle, **kwargs)Deletes the specified message from the queue.Parameters:receipt_handle: str- the receipt handle associated with the message to delete (fromtakemethod)Returns result of aiobotocore'sdelete_messagemethod.comlink.SqsConsumer(queue, handler, batch_size=1, visibility_timeout=120, wait_time_seconds=20)It is a consumer that receives messages from the queue and passes them to the handler.Init function only creates a consumer object. To start the consumer, you need to call thestartmethod.Parameters:queue: SqsQueue- queue objecthandler: Callable- a function that will be called with a message as an argument. It may be a regular function, a coroutine or an class instance with__call__method.batch_size: int- maximum number of messages to receive from the queue at oncevisibility_timeout: int- the duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by acomlink.SqsQueue.takerequestwait_time_seconds: int- the duration (in seconds) for which the call waits for a message to arrive in the queue before returning. If a message is available, the call returns sooner thanwait_time_seconds.ExamplefromcomlinkimportSqsQueue,SqsConsumer,signal_eventasyncdefhandler(message:str):print("Received message:",message)asyncdefexample(sqs_queue:SqsQueue):stop_event=signal_event()consumer=SqsConsumer(queue=sqs_queue,handler=handler)consumer_task=awaitconsumer.start(stop_event=stop_event)awaitconsumer_taskcoroutinestart(stop_event)Starts the consumer.Parameters:stop_event: asyncio.Event- a consumer will stop when this event is set. You can usecomlink.signal_eventfunction to create an event that will be set when the process receives a signal.Returns a consumer task. You can use it to wait for the consumer to stop. If the consumer is stopped by an exception, the task will raise an exception. If the consumer is stopped by thestop_event, the task will returnNone.comlink.signal_event(signals=None, log_level=logging.WARNING, loop=None)Creates an event that will be set when the process receives a signal.Parameters:signals: dict[int, str] | None- a dictionary that maps signal numbers to signal names. IfNone, the default dictionary will be used.log_level: int- a log level that will be used to log signals. Default islogging.WARNING.loop: asyncio.AbstractEventLoop | None- an event loop. IfNone, the current event loop will be used.Returns anasyncio.Eventobject.Exampleimportloggingimportsignalfromcomlinkimportsignal_eventasyncdefexample():stop_event=signal_event(signals={signal.SIGUSR1:"SIGUSR1",signal.SIGINT:"SIGINT",signal.SIGTERM:"SIGTERM",},log_level=logging.DEBUG,)awaitstop_event.wait()print("Received a signal")DevelopmentSetupInstallPoetry.Install dependencies withpoetry install.InstallDocker.Rundocker compose -f docker-compose.dev.yml up -dto start
the development environment (localstack). Tests will fail until the environment is up and running.TestingRunpoetry run pytestto run the tests.FormattingRunpoetry run black .to format the code.Runpoetry run isort .to sort the imports.