Skip to content

threadlock with basic_get #29

@pryano

Description

@pryano

When using multiple threads -- each with its own channel -- I run into a threadlock during calls to basic_get. The following code should quickly yield a threadlock. I suspect that it's because basic_get is using the channel's lock instead of a shared lock from the connection.

import logging
import threading
from threading import Thread

from amqpy import Connection

log = logging.getLogger('threadlock')
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)


def read(connection):
    thread_name = threading.current_thread().getName()
    exchange_id = 'amqp.topic'
    queue_id = 'test.queue.{}'.format(thread_name)
    routing_key = queue_id

    log.debug('Starting {}'.format(thread_name))

    channel = connection.channel()
    channel.exchange_declare(exchange_id, 'topic', durable=True, auto_delete=False)
    channel.queue_declare(queue_id)
    channel.queue_bind(queue_id, exchange=exchange_id, routing_key=routing_key)

    read_count = 0
    while True:
        messages = (channel.basic_get() for _ in range(1))
        for _ in messages:
            read_count += 1
            log.debug('{}::{}'.format(thread_name, read_count))


def main():
    connection = Connection()

    for _ in range(25):
        t = Thread(target=read, args=(connection,))
        t.start()

    while True:
        pass


if __name__ == '__main__':
    main()

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions