饭叔的知识整理

pyspider中如何运行了web提交的脚本?

module = ProjectManager.build_module(project_info, {
            'debugger': True
        })
ret = module['instance'].run_task(module['module'], task, response)

module=
{  
   'info':{  
      'status':'DEBUG',
      'name':u'aaa',
      'script':      u'#!/usr/bin/env python\n ...'
   },
   'instance':<aaa.Handler object at 0x10ee54110>,
   'exception':None,
   'load_time':1440552675.104691,
   'exception_log':'',
   'class':<class 'aaa.Handler'>,
   'module':<module 'aaa' (built-in)>,
   'loader':<pyspider.processor.project_module.ProjectLoader object at 0x10ee541d0>
}

所以运行的是aaa.Handler.run_task(aaa,task,response)

ProjectManager.build_module是如何构造的,run_task又是如何运行的?

构造:

mod = self.mod = imp.new_module(self.name)
code = self.get_code(fullname)
    six.exec_(code, mod.__dict__)
    linecache.clearcache()
    return mod

其中用到了python内部的imp和compile

def get_code(self, fullname): return compile(self.get_source(fullname), '<%s>' % self.name, 'exec')

def compile(source, filename, mode, flags=None, dontinherit=None): # real signature unknown; restored from doc__dict_ """ compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

Compile the source string (a Python module, statement or expression)
into a code object that can be executed by the exec statement or eval().
The filename will be used for run-time error messages.
The mode must be 'exec' to compile a module, 'single' to compile a
single (interactive) statement, or 'eval' to compile an expression.
The flags argument, if present, controls which future statements influence
the compilation of the code.
The dont_inherit argument, if non-zero, stops the compilation inheriting
the effects of any future statements in effect in the code calling
compile; if absent or zero these statements do influence the compilation,
in addition to any features explicitly specified.
"""
pass

run_task在哪?

pyspider.libs.base_handler def run_task(self, module, task, response):