0
import discord
import tray

I am trying to import both of these local modules, they are both scripts that run until the terminal is closed, but that means only one will run until it ends to move to the next, how can i have both of the trigger at the same time? I know i have to use multiprocessing but how so can someone help?

For example:

import X
import Y
  • so you want to import both modules in parallel? – rv.kvetch Jul 29 '22 at 17:26
  • Does this answer your question? [Why is Python running my module when I import it, and how do I stop it?](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) - by extension, move the code to run at the same time into functions, import them, and execute them with multiprocessing. – mkrieger1 Jul 29 '22 at 17:27
  • Do these modules define functions or anything like that? From the way you describe your usage of modules, it looks like you want to import a Python script just so that you can run it. – jjramsey Jul 29 '22 at 17:28
  • 1
    Yes @rv.kvetch i want to load these both at the same time, they are processes that will run over and over. So i need some way that it will run both even tho the top one will never finish. Because python reads top to bottom so it will never get to the 2nd import unless import 1 ends. – Neon Blade Jul 29 '22 at 17:32
  • The right way to fix this problem is to edit the modules `discord` and `tray`, so that their useful functionality is moved into a function. Both can then be imported without blocking the rest of the script, and you can call their functions however you want. That will not take very long and it is an improvement that should be made anyway; a module that can't be imported without major side effects is badly designed. Experienced python programmers make sure their modules are importable, if necessary by guarding code with an `if __name__ == "__main__"` block. – Paul Cornelius Jul 29 '22 at 20:15

1 Answers1

1

You can Execute multiple scripts by calling them in a bash script. For instance :

#!/bin/bash
python discord.py & 
python tray.py &

It will run the scripts in parallel.

Another way would be using the subprocess command:

import subprocess

subprocess.run("python3 discord.py & python3 tray.py", shell=True)

Using Multithreading too you can achieve this.

import discord,tray
from threading import Thread

Thread(target=discord.main).start()
Thread(target=tray.main).start()
Python Bang
  • 472
  • 3
  • 17
  • Its still only running the first one – Neon Blade Jul 29 '22 at 17:37
  • I have added one more possibility. May be you can have a try. As both the solutions worked for me – Python Bang Jul 29 '22 at 17:42
  • Nope only one worked the first one, because it wont move on until the first process finishes which it will never finish until someone closes the terminal its running in – Neon Blade Jul 29 '22 at 17:46
  • Try with Multithreading. I have modified it accordingly. If still not working. Then please post the sample code so that we can see if the issue is with the code – Python Bang Jul 29 '22 at 17:48