Like @aramcpp said, you'll need to use IPC (inter-process comm) because I believe gunicron spawns multiple processes. You could use threading module with socket module to do the communication. It looks like one process does a task, and the other grabs the result... Is that correct?
I haven't used sockets in a while so that part is pseudo code, but this is a solution:
import threading
import socket
import queue
class Sender(threading.Thread):
def __init__(self, term, q):
self.term = term
self.q = q
# < start socket connection here >
super().__init__()
def run(self):
while not self.term.is_set():
item = self.q.get(block=True)
# < convert item to byte data here >
socket.send(byte_data)
class Receiver(threading.Thread):
def __init__(self, term, q):
self.term = term
self.q = q
# < start socket connection here >
super().__init__()
def run(self):
while not self.term.is_set():
byte_data = socket.recv()
# < convert bytes to whatever >
self.q.put(item)
@app.route(/route_a)
def route_a():
q = queue.Queue()
term = threading.Event()
s = Sender(term, q)
s.start()
data = do_something()
q.put(data, block=True)
term.set()
return render_template("route_a.html")
@app.route(/route_b)
def route_b():
q = queue.Queue()
term = threading.Event()
r = Receiver(term, q)
r.start()
item = q.get(block=True) # <--- your item here
return render_template("route_b.html")
A lot of this is pseudo code, so you'll have to do some research on sockets but this is my approach. I'm sure there are other good ways to do this - or even simpler approaches.
The while loops aren't needed if it's a single shot communication, but I added them to show that a thread can run continuously to do ongoing communication.
Also, if you go this route, some resources will suggest using asyncio for IPC. I would discourage this, considering threading will never create locks. Async IPC requires more coordination.
Gunicorn documentation states " Gunicorn does not implement any IPC solution for coordinating between worker processes." So there you go. You'll need to do it yourself. Which isn't so bad because IPC is a very important skill to learn.