0

I'm making a multiplayer game on Python3. In my main menu screen, when the player clicks play I want the maingame subroutine to run, but also a completely separate file (Server.py) to run in another window parallel to it. Right now I have to open Server.py separately and then the main game file, but I want to be able to open both from the main menu.

I've tried this:

import os
import subprocess

If user_selection == "start":
    command="Server1.py"
    os.system(command)
    subprocess.Popen(command)
    main_game()

The command, os and subprocess parts are supposed to run the server1.py file (which it does). But then it proceeds to run main_game() in the same window so it stops the server and runs the game instead. I've tried using the subprocess command twice (one for server.py and other for main_game.py) but it didn't work. Is there a way when the player clicks "run game" form the main menu screen, the server file executes in a second window and the first window goes from the main menu to the main_game subroutine?

note: both files are in the same directory.

Thanks :)

1 Answer 1

1

From the info provided, it seems that concurrent.futures would be the right way to run structure your code. From the python docs

with ThreadPoolExecutor(max_workers=4) as e:
    e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
    e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
    e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
    e.submit(shutil.copy, 'src4.txt', 'dest4.txt')

The code above will copy all of the files simultaneously.

For your code, you would submit both your client and your server to the ThreadPoolExecutor.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I can understand that, but is there not an easier way like a command that I can execute to open the other py file?
Multithreading is probably the best way to go if I understand what you're trying to do. Running the files concurrently is usually best practice for client server setups. You could invoke and immediately background each piece of code (I think) but that's probably a bit complicated.
Oh ok I think I'm going to use threading. Thanks for the help.

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.