I want to run a script like this one below multiple times at the same time but using different tokens for each time, for which I already have a list of tokens.
The question is, how do I get to run this script n number of times (number of tokens), at the same time?
I have tried threading, but it didn't work, most likely due to the async functions (for which I don't have much experience). I have also tried having all the script inside one function with the token as a parameter, but the async functions were preventing it somehow, as well?
Is there some way doing this using subprocess, or such?
import module
client = module.Client()
async def on_some_event():
    do_something_using_current_client_token
def ask_for_something():
    return something
ask_for_something()
client.run(token)
Thank you.
Assuming you have a list of tokens with N items, the code below will loop through the list of tokens and run in parallel spawn N threads with the token as an argument:
import threading
import time
def doWork(token):
    count = 0
    while count < 3:
        print('Doing work with token', token)
        time.sleep(1)
        count+=1
N = 3
tokens = [n for n in range(N)]
threads = []
for token in tokens:
    thread = threading.Thread(target=doWork, args=(token,))
    thread.start()
    threads.append(thread)
for i, thread in enumerate(threads):
    thread.join()
    print('Done with thread', i)
The output:
Doing work with token 0
Doing work with token 1
Doing work with token 2
Doing work with token 1
Doing work with token 2
Doing work with token 0
Doing work with token 1
Doing work with token 2
Doing work with token 0
Done with thread 0
Done with thread 1
Done with thread 2
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With