0

I have a function in my Django Views.py and I use the data in another function but if a user changes True to False then I want to update it without having to restart Django.

def update_list():
    global processess
    processess = botactive.objects.filter(active=True).values_list('user')


update_list()

I use processess which fetches users who have a model field set to True but if they set it to False I want to not include them if there is a new request.

listi = [row[0] for row in processess] 
def wallet_verify(listi):
    # print(listi)
    database = Bybitapidatas.objects.filter(user = listi)
    ...

This is the request I make and want it to use fresh data without restarting Django and also in python and not using html.

def verifyt(request):
    with ProcessPoolExecutor(max_workers=4, initializer=django.setup) as executor:
        results = executor.map(wallet_verify, listi)
    return HttpResponse("done")
    

1 Answer 1

1

Ignoring the relative merits of globals in Django for the moment, you could just recreate the query in verifyt() to make sure its fresh.

def verifyt(request):
    v_processess = botactive.objects.filter(active=True).values_list('user')
    v_listi = [row[0] for row in v_processess] 
    
    with ProcessPoolExecutor(max_workers=4, initializer=django.setup) as executor:
        results = executor.map(wallet_verify, v_listi)
    return HttpResponse("done")

(It might be worth noting, Django queries are lazily evaluated, so, by the looks of it, your query won't actually be performed until listi is set anyway, which may do unpredictable things to your global.)

Another option might be to make your query into a function so you can call it when you need it and always get the latest

def get_listi():
    processess = botactive.objects.filter(active=True).values_list('user')
    listi = [row[0] for row in processess] 
    return listi


def verifyt(request):
    listi = get_listi()
    with ProcessPoolExecutor(max_workers=4, initializer=django.setup) as executor:
        results = executor.map(wallet_verify, listi)
    return HttpResponse("done")

def wallet_verify(user_from_listi):
    database = Bybitapidatas.objects.filter(user = user_from_listi)
    ...
Sign up to request clarification or add additional context in comments.

9 Comments

Okay - But would I still be able to use the Listi data in Wallet verify. Because I use wallet_verify for the logic of the app and then use Verifyt for multiprocessing and then I need to get the data in both of them so thought using global would be a solution. If I add the list in wallet verify would I still be able to loop through using multiprocessing - guess that is my problem
I haven't changed what you are doing outside of verifyt(). If that used to work, this approach won't change it, it merely makes sure verifyt() has the latest data. If you need the data to be fresh, though, you need to re-perform the query. Don't forget, Django will cache results of queries though (docs.djangoproject.com/en/4.1/topics/db/optimization) so if you are using listi several times at startup you may be able to re-use the the results. At other times, you probably want fresh data, so you may want a function that does the lookup and creates a new listi each time.
I see what you mean with Verifyt and that you have a fresh request but to my understanding the only reason why I have processess in verifyt is to use the list values as a range number - So that is not my issue since I can use your example for that the problem I have is that when I use it in wallet verify to actually use the data where I would do something for all the users that have set to True and Ignore false. Now users my go from True to False but I still include them since it is not fresh data. if I can refresh the request to the db without restarting django that would be great.
Added another possible solution.
All good. I have updated the answer to reflect that (calling both the list and the element of the list used as an argument in wallet_verify listi had me a bit confused)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.