0

I created 10 user accounts for an application that we have and i would like to have all these 10 users send out messages simultaneously.

so i made 10 .py scripts, in each script there is just a system call that passes `

"name of app cli client, some parameters including email and message"

to bash, then I run all of them from bash in one command using & and my question is how can I do this but do it better?

how can I send out 10 messages in parallel from bash? without creating 10 individual .py scripts? any help would be nice^^

4
  • 2
    how can I do this but do it better : what are your criteria of acceptation ? What don't you like about the default behaviour of & ? If your scripts are a simple echo, the result should be pretty much instant. Commented Sep 4, 2018 at 12:18
  • Create a list of dictionary of the parameters and loop on it. This way, 1 python script builds the 10 system calls and executes them. Commented Sep 4, 2018 at 12:22
  • I'd like to have these 10 users all send out a message simultaneously without having to create 10 individual scripts for each one user.. and my scripts are just a subprocess.check_output() with just that command in my question Commented Sep 4, 2018 at 12:22
  • Well, using a loop is not "simultaneous" theoretically, but it will be very close. Looping on 10 items is not an atomic action, but it is very small. Commented Sep 4, 2018 at 12:27

1 Answer 1

1
import threading,os

ten_python_scripts = [] # mention all files here

def func(filename):
  x = subprocess.call("python {}".format(filename), shell=True)

threads = []
for filename in ten_python_scripts:
  x = threading.Thread(target=func, args=(filename,))
  threads.append(x)
for thread in threads:
  thread.start()
for thread in threads:
  thread.join()
Sign up to request clarification or add additional context in comments.

8 Comments

thanks a lot for the answer!!! but would it also work if i just use 10 strings (my commands as strings) instead of all the file names? in ten_python_scripts ,
when I write my commands as strings it attempts to read them as file names even though they are enclosed in single quotes, what could i change so that it knows they are strings and not file names? I'm really a noob programmer and never worked with threading
can you share some code, i am not able to understand. for threading follow this tutorial simplifiedpython.net/python-threading-example
i used your code but i kept strings in my list like this ten_python_scripts = ["bash command 1", "bash command 2"] ... ( my bash commands starts with "cd ~/app-cli && some parameteres") and when i run my script i get: python: can't open file 'cd': [Errno 2] No such file or directory python: can't open file 'cd': [Errno 2] No such file or directory
i figured out what i was doing wrong, i should change python {} to just {} if i want to run strings :D thank you so much!!
|

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.