如何在Ruby中与线程通信

如何在Ruby中与线程通信

本文介绍了如何在Ruby中与线程通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个实时游戏,主要是基于聊天的,并且我需要使这些聊天中的许多并发运行,并通过Web套接字接收和发送数据.

I'm building a real time game, mostly chat based, and I need to have many of these chats running concurrently, receiving and sending data through web sockets.

有人告诉我,与其让每个游戏都产生一个进程,不如让每个游戏都带有一个线程(可能使用事件机)的进程.

I have been told that instead of spawning one process per game, I should have one process with one thread per game (maybe using Event Machine).

我在插槽部分使用剑圣,它使我可以使用发布/订阅系统将数据发送给游戏中的所有玩家:每个玩家都订阅一个游戏.但是,如何将每个玩家的数据发送到该特定游戏?

I'm using Juggernaut for the sockets part, it lets me send data to all the players in a game by using a publish/subscribe system: each player subscribes to one game. But how do I send data from each player to that particular game?

我当时想我可以将游戏ID或通道ID从客户端发送到服务器,然后将其发送到相应的线程.

I was thinking that I could send the game ID or channel ID from the client to the server, and then send it to the corresponding thread.

但是如何将任何内容发送到线程?

But how do I send anything to a thread?

推荐答案

要将数据发送到线程,可以使用Ruby Queue:

To send data to a thread, you can use Ruby Queue:

http://www.ruby- doc.org/stdlib-1.9.3/libdoc/thread/rdoc/Queue.html

这篇关于如何在Ruby中与线程通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:22