Logstash file stops running at the point when it encounters stdout.read

I am running a logstash file to extract new fields from my csv file. I have written a python script that will bring the required value from my csv file. This python script will be executed in the ruby plugin in the filter section. I was refering to the following link to get my output:

The problem I am facing is that my logstash file stops running after it gets output from python script.
The output logstash file gives is the just the output of my python script and then it stops executing.

Following is my python script:

with open('/root/local/sample.csv','r') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',')
i=0;
for row in spamreader:
if i == 0:
header=row.index(sys.argv[2]);
#print row,"\n"
else:
if row[0] == args[0] and row[2] == args[1] and row[1] == args[2]:
data = row[header]
sys.stdout.write(data)
#print "Required row: %s" % row
#print "Required Value: %s" % data
i=i+1;
csvfile.close();

Following is the filter section of my logstash file where I am executing python script:

if [RSRC_TYPE] == "Value1"
{
ruby {
code => 'require "open3"
var = event["id_val"]
result1 = "/root/local/code.py Value1,value2,#{var} value3"
stdin,stdout,stderr = Open3.popen3(result1)
event["field1"] = stdout.read
result2 = "/root/local/code.py Value1,value2,#{var} value3"
stdin,stdout,stderr = Open3.popen3(result2)
event["field2"] = stdout.read
stdout.close
stdin.close
stderr.close
'
}
}

Kindly help me to get out of this problem.

Use a translate filter instead.