0

I'm trying to pass data between two python scripts with Flask on Heroku and I think multiprocessing is the best strategy, but I'm running into an error. I've taken a simple script from another similar question and modified it as an example.

The basic idea is that in my scripts, one script gets string data from a user (like "what hotels are available?"). The second script, running flask, interprets the string ("what hotels are available?") to search a database.

In the example below, script two runs into an error, because "letters" is undefined when its called. It's not shown here, but I also tried creating a global variable in the first script and passing that to the f function (which also resulted in an error and probably wasn't the best strategy).

get_user_input.py:

from multiprocessing import Process,Queue,Pipe
from send_to_flask import f

if __name__ == '__main__':
    parent_conn,child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print(parent_conn.recv())   # prints "Hello"

send_to_flask.py:

from multiprocessing import Process,Pipe
import random
import string

def randomString(stringLength=8):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))

def f(child_conn):
    #msg = "Hello"
    msg = letters
    child_conn.send(msg)
    child_conn.close()

I also tried embedding the function in the other

def randomString(stringLength=8):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))
    f(child_conn, letters)

The problem is that one function stores a string value, but the one that passes the value to the string value (the f function) doesn't have access to the object. I want to note that in my actual program, both functions are running simultaneously, so the example above may not be an exact replica of my problem (the string would be static and externally given). I hope I made this question easy to answer and let me know if I can make it better!

tom
  • 977
  • 3
  • 14
  • 30
  • Which operating system? Windows works differently than linux/mac. – tdelaney Jun 16 '20 at 01:20
  • using mac on my local machine to test it but heroku to deploy it – tom Jun 16 '20 at 01:24
  • What version of python are you using? python 3.8 changed some multiprocessing behaviors for macos Also - it does seem like `letters` is undefined, and you are not calling `randomString` – Alonme Jun 25 '20 at 17:45
  • i'm using the latest version (i think 3.7 or 3.8, whatever is on heroku). I could downgrade if necessary – tom Jun 28 '20 at 21:13

0 Answers0