Vaibhav
Vaibhav

Reputation: 617

django huey is always returning empty queryset while filtering

@db_task()
def test_db_access(tenant_id, batch_obj):
    print('DBAccess')
    print(tenant_id)
    print(batch_obj.id)
    files = File.objects.filter(batch_id=batch_obj.id)
    print(files)

If I run this in django without django-huey, I get a filtered queryset but if I start using django-huey, I'm always getting an empty queryset. Only 'DBAccess' is getting printed and files is always '[]'.

Do I have to add other settings in settings.py? This is my current huey settings

# Huey - Task Queue
HUEY = {
    'name': 'appname',
    'consumer': {
        'workers': 4, 
        'worker_type': 'thread'
    },
    'immediate': False,
    'connection': {
        'host': RedisConfig.HOST,
        'port': RedisConfig.PORT,
    },
}

Upvotes: 0

Views: 110

Answers (1)

Radu
Radu

Reputation: 8699

You're trying to pass an object as an argument to that function and it's probably not getting serialized - huey uses pickle to serialize function call data that is passed to the consumer. Instead, change your function to accept a batch_obj identifier like this:

@db_task()
def test_db_access(tenant_id, batch_obj_id):
    print('DBAccess')
    print(tenant_id)
    print(batch_obj_id)
    files = File.objects.filter(batch_id=batch_obj_id)
    print(files)

and pass in batch_obj_id=batch_obj.id when you're calling test_db_access. Alternatively, you can write a custom serializer, but it should be much simpler to just pass the numerical identifier.

Upvotes: 0

Related Questions