Upstarting service

Hey Guys,

I ran into an issue with the latest version of Pycharm (2016.1) where I was
no longer able to debug. Turns out it was an issue with eventlet monkey
patching which I was able to resolve but it led me to wonder if I was
running our services the best way.

Below is how we are running our services with Upstart. It required a call
to monkey patch to work. Does this look ok, or should we be using the built
in nameko run to upstart?

#main.py
import eventlet

eventlet.monkey_patch(thread=False) # thread=False fixed the debugger haning

from os.path import abspath, dirname, join
import yaml

from nameko.containers import ServiceContainer
from service import AccountService

setup_dir = dirname(abspath(__file__))

import logging.config

def main():
    stream = open("config.yml", "r")
    config = yaml.load(stream)

    logging.config.dictConfig(config["LOGGING"])

    container = ServiceContainer(AccountService, config=config)
    container.start()

    try:
        container.wait()
    except KeyboardInterrupt:
        container.stop()

if __name__ == '__main__':
    main()

Any guidance would be appreciated.

Cheers,
Conor

Hi Conor,

The eventlet monkey patch is definitely required. Without it, the
greenthreads won't cooperatively yield when waiting for IO, and you'll end
up with a hanging service.

It is strange that the thread patch can't be applied under PyCharm -- that
may be an eventlet bug. Can you reproduce it with a simple example?

If you study nameko.cli.run you'll find that it looks somewhat similar to
your main.py (but using a ServiceRunner rather than a single
ServiceContainer). It is perfectly fine to build your own runner, but I
recommend you use the built-in one if you can. There is a lot of faff
around dealing with system interrupts, which you probably wouldn't want to
reimplement, and the "backdoor" feature is useful as well. If you don't
need the service discovery part you can call run() directly.

There are definitely improvements that could be made to the built-in
runner. I see you're configuring logging via dictConfig and a LOGGING key
in config.yaml... that would be a nice enhancement if you wanted to add it.

Matt.

···

On Friday, March 25, 2016 at 12:03:33 PM UTC, Conor Seabrook wrote:

Hey Guys,

I ran into an issue with the latest version of Pycharm (2016.1) where I
was no longer able to debug. Turns out it was an issue with eventlet monkey
patching which I was able to resolve but it led me to wonder if I was
running our services the best way.

Below is how we are running our services with Upstart. It required a call
to monkey patch to work. Does this look ok, or should we be using the built
in nameko run to upstart?

#main.py
import eventlet

eventlet.monkey_patch(thread=False) # thread=False fixed the debugger
haning

from os.path import abspath, dirname, join
import yaml

from nameko.containers import ServiceContainer
from service import AccountService

setup_dir = dirname(abspath(__file__))

import logging.config

def main():
    stream = open("config.yml", "r")
    config = yaml.load(stream)

    logging.config.dictConfig(config["LOGGING"])

    container = ServiceContainer(AccountService, config=config)
    container.start()

    try:
        container.wait()
    except KeyboardInterrupt:
        container.stop()

if __name__ == '__main__':
    main()

Any guidance would be appreciated.

Cheers,
Conor

Thanks Matt. I switched things over to use the run command as you suggested.

The debugging issue was fixed in the latest version of pycharm. I also
enabled "Gevent Compatible" in the python debugger settings.

···

On Friday, March 25, 2016 at 10:56:59 AM UTC-4, Matt Bennett wrote:

Hi Conor,

The eventlet monkey patch is definitely required. Without it, the
greenthreads won't cooperatively yield when waiting for IO, and you'll end
up with a hanging service.

It is strange that the thread patch can't be applied under PyCharm -- that
may be an eventlet bug. Can you reproduce it with a simple example?

If you study nameko.cli.run you'll find that it looks somewhat similar to
your main.py (but using a ServiceRunner rather than a single
ServiceContainer). It is perfectly fine to build your own runner, but I
recommend you use the built-in one if you can. There is a lot of faff
around dealing with system interrupts, which you probably wouldn't want to
reimplement, and the "backdoor" feature is useful as well. If you don't
need the service discovery part you can call run() directly.

There are definitely improvements that could be made to the built-in
runner. I see you're configuring logging via dictConfig and a LOGGING key
in config.yaml... that would be a nice enhancement if you wanted to add it.

Matt.

On Friday, March 25, 2016 at 12:03:33 PM UTC, Conor Seabrook wrote:

Hey Guys,

I ran into an issue with the latest version of Pycharm (2016.1) where I
was no longer able to debug. Turns out it was an issue with eventlet monkey
patching which I was able to resolve but it led me to wonder if I was
running our services the best way.

Below is how we are running our services with Upstart. It required a call
to monkey patch to work. Does this look ok, or should we be using the built
in nameko run to upstart?

#main.py
import eventlet

eventlet.monkey_patch(thread=False) # thread=False fixed the debugger
haning

from os.path import abspath, dirname, join
import yaml

from nameko.containers import ServiceContainer
from service import AccountService

setup_dir = dirname(abspath(__file__))

import logging.config

def main():
    stream = open("config.yml", "r")
    config = yaml.load(stream)

    logging.config.dictConfig(config["LOGGING"])

    container = ServiceContainer(AccountService, config=config)
    container.start()

    try:
        container.wait()
    except KeyboardInterrupt:
        container.stop()

if __name__ == '__main__':
    main()

Any guidance would be appreciated.

Cheers,
Conor