Code Monkey home page Code Monkey logo

Comments (2)

antoncohen avatar antoncohen commented on July 26, 2024 3

If you want to disable gevent.monkey.patch_all() in grequests (without forking), this should work:

import mock  # unittest.mock in Python 3.3+

with mock.patch('gevent.monkey.patch_all', lambda *args, **kwargs: None):
    import grequests

It is best if gevent.monkey.patch_all() is only done in application entrypoints, not libraries, because it should be done before all other imports, and should only be done once. From the gevent documentation:

Patching should be done as early as possible in the lifecycle of the program. For example, the main module (the one that tests against main or is otherwise the first imported) should begin with this code, ideally before any other imports

Even if you can't control where import grequests is done, doing the above mock.patch() in your application entrypoint should work, because Python should only import grequests once. Once it is imported that version keeps getting used, and import-time side effects of grequests shouldn't get run again.

from grequests.

spyoungtech avatar spyoungtech commented on July 26, 2024

Hi @belingud

Thanks for submitting this well-thought out issue. Using grequests together with other applications, particularly ones that also use gevent is often a source of pain when using this project. In general, we don't recommend using grequests together with such other packages and we recommend use of grequests be, more or less, standalone.

Nevertheless, such use cases seem to arise commonly.

There's a few things the stand between the current situation and a, perhaps, more desirable state. There's maybe also some alternative ways that grequests can use monkey patching to make things easier for other applications.

One suggestion has been to make the monkey patching in grequests less aggressive. I'm not sure, however, which components need to be patched by grequests. But if we could limit the patching to exactly what grequests needs, that may make it more compatible with other applications.

SInce the patch happens on import, I'm not sure what the best way would be to give users control of the monkey patch would be. I'll think some more on this. But at the end of the day, grequests does need certain things patched and may not even work correctly without those patches, which may mean that, ultimately, grequests is inherently incompatible with certain applications.

In the meantime, @belingud it is perhaps useful to know that grequests is composed of just a single file. You could try copying grequests directly to your own project (instead of installing it via pip) and modify the monkeypatching to see what may work for you! It will be useful to know, for example, whether grequests works without patching thread

from grequests.

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.