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)