如何在Python中计算字节码,以便我可以适当地修改sys.setcheckinterval

发布于 2024-07-09 04:25:35 字数 433 浏览 4 评论 0原文

我有一个使用工作队列和线程的端口扫描应用程序。

它使用简单的 TCP 连接,并花费大量时间等待数据包返回(最多半秒)。 因此,线程不需要完全执行(即前半部分发送数据包,上下文切换,执行操作,返回到有网络数据等待的线程)。

我怀疑我可以通过修改 sys.setcheckinterval 的默认值 100 来提高性能(这允许在切换到另一个线程之前执行最多 100 个字节码)。

但是,如果不知道线程或函数中实际执行了多少字节码,我就会盲目地猜测值,测试和依赖测试显示出可测量的差异(这很困难,因为执行的代码量很少;一个简单的套接字连接,因此网络抖动可能会比更改 sys.setcheckinterval 更影响任何测量。

因此,我想知道某些代码执行中有多少字节码(即函数或线程执行中的字节码总数),以便我可以更智能地猜测将 sys.setcheckinterval 设置为什么。

I have a port scanning application that uses work queues and threads.

It uses simple TCP connections and spends a lot of time waiting for packets to come back (up to half a second). Thus the threads don't need to fully execute (i.e. first half sends a packet, context switch, does stuff, comes back to thread which has network data waiting for it).

I suspect I can improve performance by modifying the sys.setcheckinterval from the default of 100 (which lets up to 100 bytecodes execute before switching to another thread).

But without knowing how many bytecodes are actually executing in a thread or function I'm flying blind and simply guessing values, testing and relying on the testing shows a measurable difference (which is difficult since the amount of code being executed is minimal; a simple socket connection, thus network jitter will likely affect any measurements more than changing sys.setcheckinterval).

Thus I would like to find out how many bytecodes are in certain code executions (i.e. total for a function or in execution of a thread) so I can make more intelligent guesses at what to set sys.setcheckinterval to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Saygoodbye 2024-07-16 04:25:35

对于更高级别(方法、类), dis 模块 应该有帮助。

但如果需要更细粒度,跟踪将是不可避免的。 跟踪确实是逐行进行的,但此处解释是一个深入研究的好方法字节码级别。 向内德·巴切尔德致敬。

For higher level (method, class) wise, dis module should help.

But if one needs finer grain, tracing will be unavoidable. Tracing does operate line by line basis but explained here is a great hack to dive deeper at the bytecode level. Hats off to Ned Batchelder.

南…巷孤猫 2024-07-16 04:25:35

对如此复杂的系统进行推理很少会得出正确的答案。 测量结果,并使用运行最快的设置。 如果如你所说,测试无法测量 setcheckinterval 各种设置的差异,那么为什么还要费心去改变它呢? 只有可测量的差异才是有趣的。 如果您的测试运行时间太短而无法提供有意义的数据,请延长运行时间直至提供有意义的数据。

Reasoning about a system of this complexity will rarely produce the right answer. Measure the results, and use the setting that runs the fastest. If as you say, testing can't measure the difference in various settings of setcheckinterval, then why bother changing it? Only measurable differences are interesting. If your test run is too short to provide meaningful data, then make the run longer until it does.

つ可否回来 2024-07-16 04:25:35

“我怀疑我可以通过修改 sys.setcheckinterval 来提高性能”

这很少起作用。 正确的行为不能取决于时间——你无法控制时间。 操作系统、硬件、Python 补丁级别或月相的轻微变化都会改变应用程序的行为方式。

select 模块是您用来等待的模块I/O。 您的应用程序可以构建为一个主循环,该循环执行选择和 为其他线程排队 工作。 其他线程正在等待其请求队列中的条目进行处理。

" I suspect I can improve performance by modifying the sys.setcheckinterval"

This rarely works. Correct behavior can't depend on timing -- you can't control timing. Slight changes on OS, hardware, patch level of Python or phase of the moon will change how your application behaves.

The select module is what you use to wait for I/O's. Your application can be structured as a main loop that does the select and queues up work for other threads. The other threads are waiting for an entries in their queue of requests to process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文