-
Notifications
You must be signed in to change notification settings - Fork 0
Cython
Here you will find guidance and tips for working on NautilusTrader using the Cython language. More information on Cython syntax and conventions can be found by reading the Cython docs. 在这里,您将找到使用 Cython 语言处理 NautilusTrader 的指南和技巧。通过阅读 Cython 文档,可以找到有关 Cython 语法和约定的更多信息。
Cython is a compiled programming language that aims to be a superset of the Python programming language, designed to give C-like performance with code that is written mostly in Python with optional additional C-inspired syntax. Cython 是一种编译型编程语言,旨在成为 Python 编程语言的超集,旨在通过主要用 Python 编写的代码(可选的附加 C 风格语法)提供类似 C 的性能。
The project heavily utilizes Cython to provide static type safety and increased performance for Python through C extension modules. The vast majority of the production code is actually written in Cython, however the libraries can be accessed from both Python and Cython. 该项目大量使用 Cython,通过 C 扩展模块为 Python 提供静态类型安全性和更高的性能。实际上,绝大多数生产代码都是用 Cython 编写的,但是可以从 Python 和 Cython 访问库。
Ensure that all functions and methods returning void or a primitive C type (such as bint, int, double) include the except * keyword in the signature. 确保所有返回 void 或原始 C 类型(例如 bint、int、double)的函数和方法都在签名中包含 except * 关键字。
This will ensure Python exceptions are not ignored, and instead are “bubbled up” to the caller as expected. 这将确保 Python 异常不会被忽略,而是按预期“冒泡”到调用者。
Improved debugging support for Cython has remained a highly up-voted PyCharm feature for many years. Unfortunately, it's safe to assume that PyCharm will not be receiving first class support for Cython debugging https://youtrack.jetbrains.com/issue/PY-9476. 多年来,对 Cython 的改进调试支持一直是 PyCharm 中获得高度支持的功能。不幸的是,可以肯定的是,PyCharm 不会获得对 Cython 调试的一流支持 https://youtrack.jetbrains.com/issue/PY-9476。
The following recommendations are contained in the Cython docs: https://cython.readthedocs.io/en/latest/src/userguide/debugging.html 以下建议包含在 Cython 文档中:https://cython.readthedocs.io/en/latest/src/userguide/debugging.html
The summary is it involves manually running a specialized version of gdb from the command line. We don't recommend this workflow. 总结是,它涉及从命令行手动运行 gdb 的专用版本。我们不推荐这种工作流程。
When debugging and seeking to understand a complex system such as NautilusTrader, it can be quite helpful to step through the code with a debugger. With this not being available for the Cython part of the codebase, there are a few things which can help: 在调试和试图理解像 NautilusTrader 这样的复杂系统时,使用调试器逐步执行代码可能非常有帮助。由于代码库的 Cython 部分没有这个功能,所以有一些方法可以帮助您:
- Ensure LogLevel.DEBUG is configured for the backtesting or live system you are debugging. This is available on BacktestEngineConfig(logging=LoggingConfig(log_level="DEBUG")) or TradingNodeConfig(logging=LoggingConfig=log_level="DEBUG")). With DEBUG mode active you will see more granular and verbose log traces which could be what you need to understand the flow. 确保为正在调试的回测或实时系统配置 LogLevel.DEBUG。这在 BacktestEngineConfig(logging=LoggingConfig(log_level="DEBUG")) 或 TradingNodeConfig(logging=LoggingConfig=log_level="DEBUG")) 上可用。在 DEBUG 模式处于活动状态时,您将看到更细粒度和更详细的日志跟踪,这可能是您理解流程所需的。
- Beyond this, if you still require more granular visibility around a part of the system, we recommend some well-placed calls to a components logger (normally self._log.debug(f"HERE {variable}" is enough). 除此之外,如果您仍然需要围绕系统的一部分进行更细粒度的可见性,我们建议对组件记录器进行一些适当的调用(通常 self._log.debug(f"HERE {variable}" 就足够了)。