Giter Club home page Giter Club logo

Comments (3)

F3ngdw avatar F3ngdw commented on June 2, 2024

Just to add:
A Ticker class can be seen in the py-bt of thread 19, actually Ticker is a tool type thread used to make timers,

class Ticker(threading.Thread):
    def __init__(self, period, callback, *args, **kwargs):
        self._run_on_start = kwargs.pop("run_on_start", True)
        super(Ticker, self).__init__(*args, **kwargs)
        self._period = period
        self._callback = callback
        self._hand = True

    def stop(self):
        self._hand = False

    def run(self):
        while self._hand:
            if not self._run_on_start:
                time.sleep(self._period)

            try:
                self._callback()
            except BaseException:
                log.error(traceback.format_exc())

            if self._run_on_start:
                time.sleep(self._period)

Is it because a certain Ticker thread did not apply a monkey patch?

from gevent.

jamadden avatar jamadden commented on June 2, 2024

Yes, a LoopExit can mean there is no other greenlet to switch to, and that in turn can be caused by only partly monkey-patching (e.g., not patching thread, but patching socket) or by using gevent objects directly but neglecting to use greenlets.

gevent version: 1.0.2
Python version: 2.7.5

That is a truly ancient version of gevent, almost 9 years old. I don't even remember enough about how it worked to hazard a guess beyond what is already documented (i.e., you have no other greenlet to switch to). I strongly encourage upgrading.

Your Python version is also not supported by recent gevent releases. The last version that supported Python 2 was from 2022, but it needs at least Python 2.7.9.

from gevent.

F3ngdw avatar F3ngdw commented on June 2, 2024

@jamadden Thank you for your reply!

and that in turn can be caused by only partly monkey-patching (e.g., not patching thread, but patching socket)

no, my project didn't patch anything

by using gevent objects directly but neglecting to use greenlets.

sorry, I don't understand what this means. In my project, the coroutine is implemented by inheriting gevent.greenlet.Greenlet
e.g.

class Persister(gevent.greenlet.Greenlet):
    def __init__(self):
        super(Persister, self).__init__()

        self._queue = gevent.queue.Queue()
        self._complete = gevent.event.Event()

        self._session = Session()

    def __getattribute__(self, item):
        """
        Wrap functions with logging
        """
        if item.startswith('_'):
            return object.__getattribute__(self, item)
        else:
            try:
                return object.__getattribute__(self, item)
            except AttributeError:
                try:
                    attr = object.__getattribute__(self, "_%s" % item)
                    if callable(attr):
                        def defer(*args, **kwargs):
                            dc = DeferredCall(attr, args, kwargs)
                            self._queue.put(dc)

                        return defer
                    else:
                        return object.__getattribute__(self, item)
                except AttributeError:
                    return object.__getattribute__(self, item)
  
    def _run(self):
        log.info("Persister listening")

        while not self._complete.is_set():
            try:
                data = self._queue.get(block=True, timeout=1)
            except gevent.queue.Empty:
                continue
            else:
                try:
                    data.fn(*data.args, **data.kwargs)
                    self._session.commit()
                except Exception:
                    # Catch-all because all kinds of things can go wrong and
                    # our behaviour is the same: log the exception, the data
                    # that caused it, then try to go back to functioning.
                    log.exception(
                        "Persister exception persisting data: %s" %
                        (data.fn,))

                    self._session.rollback()

    def stop(self):
        self._complete.set()

And I have another question, Is it necessary to apply a monkey patch when gevent and threads coexist?
Thank you again and I will be looking forward to your reply.

from gevent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.