Skip to content Skip to sidebar Skip to footer

Running Python Script Several Times With Different Parameters

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

Solution 1:

Asyncio solution:

import asyncio

asyncdefdoWork(token):
    count = 0while count < 3:
        print('Doing work with token', token)
        await asyncio.sleep(1)
        count+=1

N = 3
tokens = [n for n inrange(N)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait([doWork(token) for token in tokens]))
print('Done')

Output:

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
Doing work with token 1
Doing work with token 2
Doing work with token 0
Doing work with token 1
Done

Solution 2:

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

defdoWork(token):
    count = 0while count < 3:
        print('Doing work with token', token)
        time.sleep(1)
        count+=1

N = 3
tokens = [n for n inrange(N)]
threads = []

for token in tokens:
    thread = threading.Thread(target=doWork, args=(token,))
    thread.start()
    threads.append(thread)

for i, thread inenumerate(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

Solution 3:

I simply needed to add asyncio.set_event_loop(asyncio.new_event_loop()) at the very start, as asyncio's loops only ran in the main thread and not the new ones.

Post a Comment for "Running Python Script Several Times With Different Parameters"