简体   繁体   中英

call external function in python cgi-bin

I am trying to call a Torch 7 program from within a Python cgi-bin script.

If I run the following Python script from the command line:

# -*- coding: utf-8 -*-
from subprocess import call
call (['th', 'sample.lua', 'cv/lm_lstm_epoch3.54_0.9324.t7', '-gpuid', '-1', '-primetext', '"אמר הגאון הגרפקא המן איש טוב היה שנאמר"', '-temperature', '1.0', '-length', '1000'])

This works fine and I get the following output

ubuntu@ip-172-31-45-110:/usr/lib/cgi-bin$ python test2.py creating an lstm...

seeding with "אמר הגאון הגרפקא המן איש טוב היה שנאמר"

"אמר הגאון הגרפקא המן איש טוב היה שנאמר"זה יקרק ידו מי שכן בלבד ומחללין עליו אותוממנו וזכה זה לא סולד דינר הקדשו מחופה את אשתו מבית הרן וביניהןאבל יום אחד ולמנין שניה ימיבתן ככלום זכותדכתיב אשר בחרם אם אשר עשו לא כל חטאת קמיהבדין נקרא ולא מהוציאה ולא ניקבה לא אמר ליה אם תימצילומר עד דכם ראשון בגובהו הרי ז] נידוןטומאה מידקיסא אי תלמודה ומי איכא למיפרך מהלשרעתו לאהתנאו לא א"ל הכי קאמר אפילושחי ושוברו של מקום הראשוןכסיפו והרי חילין בבית המקדש והיכיממעיטרים אחרינא ועיבור בישראל מי איכא מידיאישתי מירחו דשקיל אמר להו אי שבעים שנה שעוריםעלין א"כ רבי ינא י בר מישראלאמר לו נפש גזר קונשי ב תרצות

However if I try calling Torch 7 from a cgi-bin script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import os, sys

import cgi, cgitb


print "Content-Type: text/html"
print                
print "<TITLE>CGI script output</TITLE>"

output = ""

output = subprocess.call (['th', 'sample.lua', 'cv/lm_lstm_epoch3.54_0.9324.t7', '-gpuid', '-1', '-primetext', '"אמר הגאון הגרפקא המן איש טוב היה שנאמר"', '-temperature', '1.0', '-length', '1000'])

print output

Then I get no output at all to the webpage, there is also no error in the apache error log.

Thanks

You want to use check_output to store the output of the command you are executing. call will not do this. Call will only give you the return code of what you are executing.

subprocess.check_output(['th', 'sample.lua', 'cv/lm_lstm_epoch3.54_0.9324.t7', '-gpuid', '-1', '-primetext', '"אמר הגאון הגרפקא המן איש טוב היה שנאמר"', '-temperature', '1.0', '-length', '1000'])

May be you could try lutorpy then you can run the torch code directly with python. You can use require("sample") to import your sample.lua module, and then run the inner function just like you run a python function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM