I’m new to python and apologies if my way of questioning is wrong.
I have two python files. one file will take 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 and it takes input from python file one and processes and picks the second incoming JSON output from python file one. This process continues till the python one file ends its loop
**pythonone.py file
**
import json, time
from faker import Faker
#Create a Faker object to generate fake data for the 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 on 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
2
Answers
Your
main
needs to return some data, so the caller can use them, also use better namesjson_obj_list
at each iteration, you need to collect them in a listi
in yourmyrandomdata
, and you can don’t need faker for such random intyou should bring in
threading
conceptcondition variable
allows one or more threads to wait until they are notified by another thread