#总结
C# .Net Framework 3.5
Python 26或者27
C#调用方式
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "python.exe";
//no create the cmd windows
processStartInfo.Arguments = Command;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;
try
{
Console.WriteLine("Class Execute Method exec_python Debug:process.Start");
process.Start();
Console.WriteLine("Class Execute Method exec_python Debug:read process end");
Output = process.StandardOutput.ReadToEnd();
}
##起因
在调整Tactic的外部程序,更新了一些如EXR文件读取、色彩空间、时间码等内容。由于Tactic的最新稳定版本是Tactic_v4.1.0.v05,对应配置了Windows的Python环境。当EXR部分执行通过沾沾自喜的时候,Tactic Checkin上传文件发生错误。诡异的是,上传部分代码没有做过变更,很久之前就可以正常使用了。随即决定好好跟踪下Python代码,看下Tactic Client发送文件的原理。
##checkin.py的调试
from tactic_client_lib import TacticServerStub
#...
try:
snapshot = server.simple_checkin(search_key,context,path,description=desc,mode="upload",create_icon=True);
value = "checkin:OK";
except:
value = "checkin:ERROR";
print value;
##跟踪到tactic_server_stub.py
print "Class tactic_server_stub Method simple_checkin Debug:run";
mode_options = ['upload', 'uploaded', 'copy', 'move', 'local','inplace']
print "Class tactic_server_stub Method simple_checkin Debug:[mode_option]:" + mode;
if mode:
if mode not in mode_options:
raise TacticApiException('Mode must be in %s' % mode_options)
if mode == 'upload':
print "Class tactic_server_stub Method simple_checkin Debug:run upload_file function [file_path]:" + file_path;
my.upload_file(file_path)
跟踪到upload_file函数
def upload_file(my, path):
'''API Function: upload_file(path)
Use http protocol to upload a file through http
@param:
path - the name of the file that will be uploaded
'''
print "Class tactic_server_stub Method upload_file Debug:run";
from common import UploadMultipart
print "Class tactic_server_stub Method upload_file Debug:import UploadMultipart from common";
upload = UploadMultipart()
print "Class tactic_server_stub Method upload_file Debug:[my.transaction_ticket]:" + my.transaction_ticket;
upload.set_ticket(my.transaction_ticket)
if my.server_name.startswith("http://") or my.server_name.startswith("https://"):
upload_server_url = "%s/tactic/default/UploadServer/" % my.server_name
else:
upload_server_url = "http://%s/tactic/default/UploadServer/" % my.server_name
print "Class tactic_server_stub Method upload_file Debug:[upload_server_url]:" + upload_server_url;
upload.set_upload_server(upload_server_url)
print "Class tactic_server_stub Method upload_file Debug:[path]:" + path;
upload.execute(path)
##跟踪到UploadMultipart
def execute(my, path):
print "Class UploadMultipart Method execute Debug:[path]:" + path;
assert my.server_url
f = open(path, 'rb')
print "Class UploadMultipart Method execute Debug:import codecs";
import codecs
#f = codecs.open(path, 'rb')
print "Class UploadMultipart Method execute Debug:end codecs";
count = 0
while 1:
buffer = f.read(my.chunk_size)
print "Class UploadMultipart Method execute Debug:[while.f.read]";
if not buffer:
break
if count == 0:
action = "create"
else:
action = "append"
fields = [
("ajax", "true"),
("action", action),
]
if my.ticket:
print "Class UploadMultipart Method execute Debug:[my.ticket]:" + my.ticket;
fields.append( ("ticket", my.ticket) )
fields.append( ("login_ticket", my.ticket) )
basename = os.path.basename(path)
print "Class UploadMultipart Method execute Debug:[basename]:" + basename;
from json import dumps as jsondumps
print "Class UploadMultipart Method execute Debug:import json";
basename = basename.decode(sys.stdout.encoding)
print "Class UploadMultipart Method execute Debug:[basename.decode]:" + basename;
才正式跟踪到了异常退出的语句
basename = basename.decode(sys.stdout.encoding)
##结论 basename = basename.decode(sys.stdout.encoding)
为Python 2所具备的函数,通过Python26 和 Python 27同样调用该函数时,均发生C# Process异常退出的情况。 判断,为Python lib库中的BUG,确定Python在某些情况下的稳定性测试,并不完备
##吐槽 话说Python代码上千了之后,什么鬼!超长的if else 都不知道谁是谁。