0

I am not able to understand why I am getting duplicate ID's for asyncio queues, I am not able to understand why some of them have repeated object ID's.

I am using python3.6.7

#######REPRODUCIBLE CODE#######
from asyncio import Queue, LifoQueue 


class System:
    def __init__(self, system_name):
        self.app = {}
        self.app['event_queue'] = self.build_queue('fifo')
        self.app['heartbeat_queue'] = self.build_queue('fifo')

    def build_queue(self, consumer_queue_strategy):
        if consumer_queue_strategy == 'fifo':
            return Queue()
        if consumer_queue_strategy == 'lifo':
            return LifoQueue()
        raise ValueError(f'Invalid queue strategy "{consumer_queue_strategy}"')

for dv_system in [('UATDSG'),('UATVAIBHAV'),('UATP114271571'),('UATORION18')]:
    system_name = dv_system
    sys =  System(system_name)
    print(system_name,'HB Q',id(sys.app['heartbeat_queue']))
    print(system_name,'SYS Q',id(sys.app['event_queue']))
OUT===>
UATDSG HB Q 4410129656
UATDSG SYS Q 4410129488
UATVAIBHAV HB Q 4409567608
UATVAIBHAV SYS Q 4409568000
UATP114271571 HB Q 4410129488
UATP114271571 SYS Q 4410129656
UATORION18 HB Q 4409568000
UATORION18 SYS Q 4409567608

Artemiy Rodionov I have done the following, but I am not getting correct results,

systems = []
# for dv_system in loop.run_until_complete(get_all_routerbound_allowed_systems_on_init(app)):
for dv_system in [('UATDSG'),('UATVAIBHAV'),('UATP114271571'),('UATORION18')]:
    system_name = dv_system
    systems.append(System(app, system_name))

   for sys in systems:
        print(id(sys),'HB Q',id(sys.app['heartbeat_queue']))
        print(id(sys),'SYS Q',id(sys.app['event_queue']))

which logs the following, where the objects are differnt but are referring to same Queue objects ?

4493695632 HB Q 4493695856
4493695632 SYS Q 4493695744

4493695912 HB Q 4493695856
4493695912 SYS Q 4493695744

4493695800 HB Q 4493695856
4493695800 SYS Q 4493695744

4493695688 HB Q 4493695856
4493695688 SYS Q 4493695744
vector8188
  • 1,293
  • 4
  • 22
  • 48

1 Answers1

0

CPython id uses the memory location as the object id, so object's id is unique only for the lifetime of the object. After the object is destroyed something else can have the same id.

You can see this question for details.

In your case System instances have too short lifetimes, bounded by iteration, so they have same ids.

System instances with different lifetimes have unique ids. You can check it:

from asyncio import Queue, LifoQueue


class System:
    def __init__(self, system_name):
        self.app = {}
        self.app['event_queue'] = self.build_queue('fifo')
        self.app['heartbeat_queue'] = self.build_queue('fifo')

    def build_queue(self, consumer_queue_strategy):
        if consumer_queue_strategy == 'fifo':
            return Queue()
        if consumer_queue_strategy == 'lifo':
            return LifoQueue()
        raise ValueError(f'Invalid queue strategy "{consumer_queue_strategy}"')
systems = []
for dv_system in [('UATDSG'),('UATVAIBHAV'),('UATP114271571'),('UATORION18')]:
    system_name = dv_system
    systems.append(System(system_name))
for sys in systems:
    print(system_name,'HB Q',id(sys.app['heartbeat_queue']))
    print(system_name,'SYS Q',id(sys.app['event_queue']))
Artemij Rodionov
  • 1,721
  • 1
  • 17
  • 22