简体   繁体   中英

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 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

Asyncio solution:

import asyncio

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

N = 3
tokens = [n for n in range(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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM