当前位置: 首页>>代码示例>>Python>>正文


Python Job.transaction方法代码示例

本文整理汇总了Python中models.Job.transaction方法的典型用法代码示例。如果您正苦于以下问题:Python Job.transaction方法的具体用法?Python Job.transaction怎么用?Python Job.transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Job的用法示例。


在下文中一共展示了Job.transaction方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: submit

# 需要导入模块: from models import Job [as 别名]
# 或者: from models.Job import transaction [as 别名]
def submit( request):
    # Verify the that we have the proper request parameters
    if request.method != 'POST':
        return HttpResponse( err_not_get(), status=400)  # Bad request
    
    (trans, error_response) = validate_trans_id( request)
    if error_response != None:
        # TransID didn't validate...
        return error_response
    
    if not 'NumNodes' in request.POST:
        return HttpResponse( err_missing_param( 'NumNodes'), status=400)  # Bad request
    
    if not 'CoresPerNode' in request.POST:
        return HttpResponse( err_missing_param( 'CoresPerNode'), status=400)  # Bad request
    
    # Verify the request parameters for the python script
    if not 'ScriptName' in request.POST:
        return HttpResponse( err_missing_param( 'ScriptName'), status=400)  # Bad request
    
    script_name = request.POST['ScriptName']
    if not script_name in request.POST:
        return HttpResponse( "Expected POST variable %s not received"%script_name, status=400)  # Bad request
       
    # Make sure we don't overwrite an existing script with a new one of the same name
    # TODO: Technically, this is a race condition: it's theoretically possible for a user
    # to send 2 requests simultaneously with the same script name and have one overwritten
    # because neither existed at the time isfile() was called.  Not sure what to do about
    # this: some web servers spawn multiple processes, so a regular mutex or critical
    # section won't help.
    full_script_name = os.path.join( trans.directory, script_name)
    file_num=0
    while (os.path.isfile( full_script_name)):
        file_num += 1
        full_script_name = os.path.join( trans.directory, "%s-%d"%(script_name, file_num))
    
    # Save the uploaded python script to the transaction directory    
    script_file = open( full_script_name, 'w')
    script_file.write( request.POST[script_name])
    script_file.close() 

    # Generate the bash script that will actually be run by Moab/Torque
    # TODO: see the comments above about the race condition with file names
    submit_file_name = os.path.join( trans.directory, 'submit.sh')
    file_num=0
    while (os.path.isfile( submit_file_name)):
        file_num += 1
        submit_file_name = os.path.join( trans.directory, 'submit.sh-%d'%file_num) 
    submit_file = open(submit_file_name, 'w')
    submit_file.write( generate_bash_script( NUM_NODES=request.POST['NumNodes'],
                                             CORES_PER_NODE=request.POST['CoresPerNode'],
                                             TRANSACTION_DIRECTORY=trans.directory,
                                             PYTHON_JOB_SCRIPT=os.path.basename(full_script_name)))
    submit_file.close()
    
    # Generate the JSON that's submitted to Moab Web Services
    submit_json = {}
    submit_json['commandFile'] =  "/bin/bash"
    submit_json['commandLineArguments'] = submit_file.name
    submit_json['user'] = request.user.username
    submit_json['group'] = 'users'
    
    # The job name parameter is optional
    if 'JobName' in request.POST:
        submit_json['name'] = request.POST['JobName']
    else:
        submit_json['name'] = "Unknown"
    
    num_cores = int(request.POST['NumNodes']) * int (request.POST['CoresPerNode'])
    submit_json['requirements'] = [ {"requiredProcessorCountMinimum": num_cores, "tasksPerNode": request.POST['CoresPerNode']} ]                                
    # For reasons that have never been clear, MWS wants the requirements
    # field to be a list containing a single object (instead of just the
    # object itself...)
    
    # This isn't necessary for Moab to schedule the job.  However, by setting
    # the variable, we can distinguish between jobs that were submitted via
    # this mechanism and stuff that the user might have just qsub'd...
    # Note: Last I knew, a bug in MWS meant these variables were forgotten.
    # Not sure if this has been fixed yet.
    submit_json['variables'] = {"JOB_TYPE":"Mantid"}
    submit_json['standardErrorFilePath'] = trans.directory
    submit_json['standardOutputFilePath'] = trans.directory
    
    # This is just an example so I remember how to set environment variables in case I
    # ever need to.  Leave it commented out.
    #submit_json['environmentVariables'] = {"MANTIDPLOT_NUM_NODES" : request.POST['NumNodes']}
    
    # Make the HTTP call
    return_code,json_result = mws_request(settings.MWS_URL + "/jobs", submit_json)
    if return_code == 201:
        # Success Return the Job ID to the user
        json_out = { }
        json_out['JobID'] = json_result['id']
        
        # Add the job object to the local db
        new_job = Job()
        new_job.transaction = trans
        new_job.script_name = script_name
        new_job.mws_job_id = json_result['id'];
        new_job.save()
#.........这里部分代码省略.........
开发者ID:neutrons,项目名称:MantidRemote,代码行数:103,代码来源:views.py


注:本文中的models.Job.transaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。