Home > Software design >  two python files - one file gives json object and another file takes the output as input
two python files - one file gives json object and another file takes the output as input

Time:12-26

I'm new to python and apologies if my way of questioning is wrong.

I have two python files. one file will takes as input and another file's output. Problem is, I need to pass input one at a time. So python one file will have a for loop and it generates JSON one by one and it should pass JSON one by one in that for loop only. It means, python file two will be running ang it takes input from python file one and process that and picks second incoming JSON output from python file one.. This process continues till python one files ends its loop

**pythonone.py file **

import json,time
from faker import Faker

#Create Faker object to generate fake data for Producer
fake=Faker()

def myrandomdata(i,j):
    return fake.random_int(min = 1, max = j)

json_obj_list = []
random_ins_id = str(myrandomdata(20000,10000000))
random_inv_item_id = str(myrandomdata(20000,10000000))
random_inv_org_id = str(myrandomdata(1000,100000))
random_loc_id = str(myrandomdata(20000,100000))
qty = myrandomdata(1,100)
loc_type_id = myrandomdata(0,4)

def main():
    for i in range(5):
        json_obj_list={'ID': random_ins_id,
            'QTY': qty,
            'EXT_REF': random_loc_id,
            'INV_ITEM_ID': random_inv_item_id,
            'ORG_ID': random_inv_org_id,
            'SERIAL_NUMBER': loc_type_id
            }
    json_dump = json.dumps(json_obj_list, indent="\t")
    print(json_dump)
    time.sleep(3)

**Pythontwo.py **

def process_my_data:
    res= pythonone.main()
    /*i do some process */

Guide me how can i achieve this

I am stuck to wait one file to process and pick second one and then 3rd one and stops till for loop ends from pythonone.py

CodePudding user response:

you should bring in threading concept

  • create a producer method for producing JSON
  • create a consumer method for consuming that JSON

condition variable allows one or more threads to wait until they are notified by another thread

import json,time
from threading import *
from faker import Faker

#Create Faker object to generate fake data for Producer
fake=Faker()

def myrandomdata(i,j):
    return fake.random_int(min = 1, max = j)

li = [ ]
random_ins_id = str(myrandomdata(20000,10000000))
random_inv_item_id = str(myrandomdata(20000,10000000))
random_inv_org_id = str(myrandomdata(1000,100000))
random_loc_id = str(myrandomdata(20000,100000))
qty = myrandomdata(1,100)
loc_type_id = myrandomdata(0,4)

def produce():
    for i in range(3):
        condition_object.acquire()
        json_obj_list={'ID': random_ins_id,
            'QTY': qty,
            'EXT_REF': random_loc_id,
            'INV_ITEM_ID': random_inv_item_id,
            'ORG_ID': random_inv_org_id,
            'SERIAL_NUMBER': loc_type_id
            }
        print("produced json",json_obj_list)
        li.append(json_obj_list)
        condition_object.notify()
        condition_object.wait()
        
def consume():
    for i in range(3):
        condition_object.acquire()
        json = li.pop()
        print("the json for consuming ", json)
        condition_object.notify()
        condition_object.wait()
        
        
condition_object = Condition()
T1 = Thread(target=produce)

T2 = Thread(target=consume)

T1.start()

T2.start()
  • the produces acquires the object and adds the JSON to a common list and notify the consumer and wait for call back
  • while the consumer acquires the object gets the JSON and notify the producer to continue from waiting state while the consumer being in waiting state .

CodePudding user response:

Your main needs to return some data, so the caller can use them, also use better names

  • you're overwritting json_obj_list at each iteration, you need to collect them in a list
  • you're generating the random number once, you need to do it for each iteration
  • you're not using i in your myrandomdata, and you can don't need faker for such random int

from random import randrange

def myrandomdata(i, j):
    return randrange(i, j   1)

def generate_items():
    json_obj_list = []
    for i in range(5):
        json_obj_list.append({'ID': myrandomdata(20000, 10000000),
                              'QTY': myrandomdata(1, 100),
                              'EXT_REF': myrandomdata(20000, 100000),
                              'INV_ITEM_ID': myrandomdata(20000, 10000000),
                              'ORG_ID': myrandomdata(1000, 100000),
                              'SERIAL_NUMBER': myrandomdata(0, 4)})
    return json_obj_list
def process_my_data():
    res = pythonone.generate_items()
    for item in res:
        print(type(item), item)
  • Related