Hi, I am new to Python + CGI. my application is to searching multiple file using parallel processing. each process return the output any time. I want to process that ourput and place in HTML table. here is mycode.
what I abserved the html table is updating proper if print before "if __name__ == '__main__':".
Can you point me what is the mistake in the script?
Top CGI (page.cgi):
what I abserved the html table is updating proper if print before "if __name__ == '__main__':".
Can you point me what is the mistake in the script?
Top CGI (page.cgi):
#!/usr/bin/python import cgi, cgitb cgitb.enable() print ("Content-type:text/html\r\n\r\n"); print (''); print ('<html>'); print ('<form action = "./searchEngine.cgi" method = "post">'); print ('First Name: <input type = "text" name = "first_name"> <br />'); print ('Last Name: <input type = "text" name = "last_name" />'); print ('<input type = "submit" value = "Submit" />'); print ('</form>'); print ('</html>');searchEngine.cgi:
#!/usr/bin/python import multiprocessing as mp import re import os import sys import linecache import cgi, cgitb cgitb.enable() results = []; # Create instance of FieldStorage #form = cgi.FieldStorage() # Get data from fields #first_name = form.getvalue('first_name') #last_name = form.getvalue('last_name') def FileSearch(fileName,pattern): fName = ""; lineNo = ""; regNam = "" f = open(fileName,"r+" ) for lines in f.readlines(): matchObj = re.search(pattern,lines) regName = re.search("^time",lines) fileN = re.search("^[place]",lines) st = str.split(lines,":"); if regName: regN = len(st); regNam = st[regN-1]; if fileN: fileNN = len(st); fName = st[fileNN-2]; lineNo = st[fileNN-1]; if matchObj: lineNo = int(st[0]); tp = linecache.getline(fName,lineNo); return(regNam.strip() ,fName.strip(),st[0],tp.strip()); f.close(); def collect_result(result): global results sd = result; if sd != None: results.append(result); print('<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % ( sd[0], sd[1],sd[2],sd[3])); [b[i]]#### this print is not working[/i]….[/b] def main(): ### print('<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % ( sd[0], sd[1],sd[2],sd[3])); ## this is working if __name__ == '__main__': global results; pool = mp.Pool(mp.cpu_count()); f = open("./list","r" ); data = f.readlines(); pool = mp.Pool() for i, row in enumerate(data): pool.apply_async(FileSearch,args=(row.strip(),"name"), callback=collect_result); pool.close(); pool.join(); # Wait for all subprocesses to finish. f.close(); for sd in results: if sd != None: print("<tr> <td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % ( sd[0], sd[1],sd[2],sd[3])); def htmlHead(): print ("Content-type:text/html\r\n\r\n"); print (''); print('<html>'); print('<head>'); print('<title>Python CGI test</title>'); print('</head>'); print('<body>'); print('<table id="myTable1" border = "1" width = "35%" style="margin-top:10px; margin-left:100px;">'); print('<tr>'); print('<th id=100> Name </th>'); print('<th id=101> Place </th>'); print('<th id=102> Time </th>'); print('</tr>'); def htmlTail(): print('</table>'); print('</body>'); print('</html>'); htmlHead(); main(); htmlTail();