Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with Venice #13

Open
Dev1an opened this issue Jun 5, 2016 · 1 comment
Open

Not working with Venice #13

Dev1an opened this issue Jun 5, 2016 · 1 comment

Comments

@Dev1an
Copy link
Contributor

Dev1an commented Jun 5, 2016

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?

@goloveychuk
Copy link
Owner

@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%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants