-1

I've found running different code across separate console windows in Spyder to be a handy way of executing code concurrently. I've always done this "manually" (hitting the new console button and then starting the desired piece of code in that console). However, I was wondering if there is a way to automate this process (or achieve the same effect, i.e. concurrent code with separate namespaces, in an automated way). By automated I mean something along the lines of pressing one button and having one part of the code run in one console, another part in another, and so on for a handful of consoles.

The reason I want to do this is that I am trying to run code using the zmq package and I need to have the server script and the multiple client scripts running separately from each other. I may be approaching this in a very naive way so perhaps there is a different way of doing this that doesn't require multiple consoles. I've heard the term "threading" thrown around but I'm not sure this is what I want.

Here is a specific example of why I want to be able to launch code in separate consoles automatically.

I will have one Client script that I want to be able to communicate with multiple Server scripts. Example code for what the client script and various server scripts will do is given below. This example code is very simple, but in reality the server scripts will be running calculations using a different python package. The nature of the specific calculations necessitates the use of multiple server scripts each running in their own separate namespace (or consoles). That's just a requirement of my workflow. **My question is how can I launch the various server scripts automatically without having to manually open a new spyder console for each and run the code with the specific port number?

# Client
import zmq
import time

for request in range(10):
    
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect("tcp://localhost:"+str(5555+request))
    
    print("Sending request {} ...".format(request))
    socket.send(b"Hello")
    message = socket.recv()
    
    time.sleep(0.001)
    print("Received reply {} [ {} ]". format(request, message))

.

# Server
import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:"+str(portNumber))
for i in range(10):    
    message = socket.recv() 
    print("Received request: {}".format(message))
    time.sleep(0.001)
    message = b"World"+" from server# "+str(portNumber)
    socket.send(message)
sgp45
  • 99
  • 2
  • 1
    The `threading`, `multiprocessing`, and `asyncio` modules are all possibilities, depending on what exactly you're doing. Running a script multiple times using a shell script is also possible. Without more information on what *specifically* you're trying to accomplish, it's hard to give specific advice. I'd take a look at `threading` or shell scripting first. IMO, Docker is overkill. – MattDMo Jan 12 '23 at 19:42
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jan 12 '23 at 19:48

1 Answers1

1

You should be looking into docker! It is a great way to run multiple scripts at the same time.

https://www.docker.com/

DeckChain
  • 75
  • 2
  • 10