django shell_plus timeit调用_命令?

django shell_plus timeit调用_命令?,django,django-extensions,django-commands,Django,Django Extensions,Django Commands,我已经编写了一个django命令,希望看到它的计时,因此在django shell_plus中,我执行以下操作: import timeit from django.core.management import call_command timeit.timeit("""call_command('prova')""", number=3 ) 该命令应运行3次并输出其运行时间 如果直接运行“call_command”,它会工作,如果在时间内调用,它会抛出以下错误: /usr/lib/pyth

我已经编写了一个django命令,希望看到它的计时,因此在
django shell_plus
中,我执行以下操作:

import timeit
from django.core.management import call_command

timeit.timeit("""call_command('prova')""", number=3 )
该命令应运行3次并输出其运行时间

如果直接运行“call_command”,它会工作,如果在时间内调用,它会抛出以下错误:

/usr/lib/python3.5/timeit.py in timeit(self, number)
    176         gc.disable()
    177         try:
--> 178             timing = self.inner(it, self.timer)
    179         finally:
    180             if gcold:

/usr/lib/python3.5/timeit.py in inner(_it, _timer)

NameError: name 'call_command' is not defined

timeit
始终在新的执行上下文中运行,它无法访问您以前导入的任何内容。您需要使用设置代码传递一个单独的参数:

timeit.timeit("call_command('prova')", number=3, setup='from django.core.management import call_command' )

请参阅。

timeit
始终在新的执行上下文中运行,它无权访问您以前导入的任何内容。您需要使用设置代码传递一个单独的参数:

timeit.timeit("call_command('prova')", number=3, setup='from django.core.management import call_command' )