Home > OS >  How to create multiple terminals in python (VScode)
How to create multiple terminals in python (VScode)

Time:06-29

how to create multiple terminals in python (VScode) to run the same code in several terminals simultaneously the same code. I also need to know how to open several .py files simultaneously too (running at the same time).

I found some ways to run .py files similar to:

start /b python bot_1

or

start bot_1
start bot_2

or even imports of these bots without using patch

import bot1, bot2

but it does not work. I tried with shell but I couldn't get it to work either.

CodePudding user response:

Try multiprocessing to deliver data to other cpu, run them at same time.

Notice that deliver data will spend some time.

If your bots are simple or don't need to sleep, run them one by one will be faster.

bot1.py

from time import sleep

def bot1(num):
    sleep(2)
    print(f'This is bot1, num: {num}')

bot2.py

from time import sleep

def bot2():
    sleep(2)
    print('This is bot2')

main.py

import multiprocessing as mp
import bot1, bot2

if __name__ == '__main__':
    process_list = []
    for i in range(5):
        process_list.append(mp.Process(target=bot1, args=(i,)))
    process_list.append(mp.Process(target=bot2))

    for p in process_list:
        p.start()
    for p in process_list:
        p.join()

output

[Running] python -u "d:\Documents\python\projects\test\main.py"
This is bot1, num: 0
This is bot1, num: 2
This is bot1, num: 3
This is bot1, num: 1
This is bot2
This is bot1, num: 4

[Done] exited with code=0 in 2.257 seconds

CodePudding user response:

enter image description here

You can click the plus sign on the terminal to open a new terminal.

While running a file, you can manually enter the code execution file in the other terminal.

For example,

from time import sleep


def a():
sleep(10)
print("sleep")

a()

For one, we can use Run Python File

enter image description here

For another, we can use command python testA.py in another terminal. enter image description here

  • Related