ZeroMQ
ZeroMQ copied to clipboard
Not working with Venice
When I call socket.receiveString()
in a Venice Coroutine, the app blocks inside the coroutine.
Example
When I run the following code, it prints running coroutine
and then blocks.
import ZeroMQ
import Venice
let context = try Context()
let inbound = try context.socket(.Pull)
try inbound.bind("tcp://127.0.0.1:5555")
co {
print("running coroutine")
do {
while let data = try inbound.receiveString() where data != "Bye!" {
print(data)
}
} catch {
print("An error occured")
}
}
print("sending messages outside the coroutine")
let outbound = try context.socket(.Push)
try outbound.connect("tcp://127.0.0.1:5555")
try outbound.sendString("Hello World!")
try outbound.sendString("Bye!")
Am I using Venice wrong or is this a bug?
@Dev1an Because in example above it don't used venice polling. So it's blocks a thread. To avoid this you should use Venice.poll E.g.
public class ZmqConnection {
let socket: ZeroMQ.Socket
let address: String
var polled: Venice.PollEvent? = nil
var fd: Int32 {
return try! socket.getFileDescriptor()
}
init(address: String, type: SocketType) {
self.address = address
socket = try! context.socket(type)
}
public func connect() throws {
try socket.connect(address)
}
func read() -> [Data]? {
if polled == nil {
polled = try! Venice.poll(fd, for: .reading)
guard polled!.contains(.reading) else {
return nil
}
}
let events = try! socket.getEvents()!
if events.contains(.In) {
var msg_parts: [Data] = []
while true {
guard let array = try! socket.receive() else {
break
}
msg_parts.append(array)
if !(try! socket.getReceiveMore()) {
break
}
}
return msg_parts
} else {
polled = nil
return nil
}
}
}
But I'm not sure it's works 100%