扭曲线程 TCP 客户端

发布于 2024-09-27 13:38:10 字数 243 浏览 1 评论 0原文


我正在尝试使用 Twisted python 编写一个简单的 TCPServer 和一个客户端。 一切都运转良好;但是,有没有办法将某些任务推迟到不同的线程?例如,是否可以这样做:

  • 从用户处获取输入直到\n,然后将数据发送到服务器;
  • 接受来自服务器的所有传入消息并写入屏幕;

同时?
哪些是最佳实践?

谢谢您的帮助。
——多诺万

I am trying to write a simple TCPServer and a client with Twisted python.
Everything is working well; but, there is a way to defer some tasks to different threads? For example, is it possible to do:

  • take an input from the user until \n, then send the data to the server;
  • accept all the incoming messages from the server and write to the screen;

simultaneously?
Which are best practices?

Thank you for your help.
—Donovan

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

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

发布评论

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

评论(1

终止放荡 2024-10-04 13:38:10

线程是同时执行这些操作的一种实现策略。 Twisted 通常采用另一种策略 - 非阻塞 I/O 和事件多路复用器(例如 select(2))。

如果您想在 TCPServer 运行时处理来自 stdin 的输入,则意味着您想使用 Twisted 的 API 来读取 stdin,就像您使用 Twisted 的 API 来处理网络连接一样。

请参阅twisted.internet.stdio.StandardIO 来了解这一点。

Threads are one implementation strategy for doing these things simultaneously. Twisted generally goes with another strategy - non-blocking I/O and an event multiplexer (eg select(2)).

If you want to handle input from stdin while you have a TCPServer running, all that means is that you want to use Twisted's APIs for reading from stdin, just like you're using Twisted's APIs for handling network connections.

See twisted.internet.stdio.StandardIO for that.

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