-1

I have created a Python script that I want to execute multiple times - at the same time. How do I do this?

I am writing automation scripts in Python (using Selenium chromedriver) and have a script that goes to a website and creates an account. I want to create some kind of batch file that executes this script an X amount of times simultaneously so that I can create more than one account at once on this website.

Ideally, I would like an input when the program starts that asks how many accounts you would like to create, and then the input is how many simultaneous scripts will be run. E.g.

import os 
number = input("How many guest users do you want to create? ")
run_test = os.system('python create_guest_user.py')
number = int(number)

# run the 'create_guest_user.py' script 'number' amount of times simultaneously
# (unsure on how to do this part)

I am unsure on the methods needed to execute this process.

Squashman
  • 13,649
  • 5
  • 27
  • 36
Sam1811x
  • 33
  • 1
  • 1
  • 3
  • This has been asked before, check out [this post](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) to learn about threading – aaaakshat Feb 15 '19 at 15:13
  • Use a shell: `for ((i=0; i < $n; i++)); do create_guest_user.py & done;` – William Pursell Feb 15 '19 at 15:13
  • others have mentioned threading and multiprocessing, which are good tools to use. In a one-off situation, i just created multiple consoles and ran the script in each of them. this isnt a good solution, but can be an occasionally useful hack – Zulfiqaar Feb 15 '19 at 15:36

1 Answers1

0

Its my understanding that Python by default uses a Global Interpreter Lock (GIL) which restricts the number of processes that can be run at once.

If the machine you are going to run this script on has multiple cores than you should look at the multiprocessing library as this could be used to run each script on a different core... the multithreading library may also be of use however this will only work if the interpreter is able to switch between processes if there is idle time...

Here is a previous answer that may help you.

execute python script multiple times

Also the comments made on your question also contain some good ideas and links...

You can run multiple instances of a python script from a shell however from within a python program without the use of multithreading/multiprocessing the GIL limitation will impact what you are trying to do.

Mark Smith
  • 757
  • 2
  • 16
  • 27