Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Apr 09 08:37
    webknjaz commented #1872
  • Mar 30 08:56
    webknjaz locked #1905
  • Mar 30 08:56
    webknjaz labeled #1905
  • Mar 30 08:55
    webknjaz closed #1905
  • Mar 30 08:55
    webknjaz commented #1905
  • Mar 30 07:23
    Wissperwind edited #1905
  • Mar 29 15:34
    Wissperwind opened #1905
  • Mar 24 12:21
    webknjaz edited #1904
  • Mar 24 12:20
    webknjaz commented #1904
  • Mar 23 14:39
    mpf82 opened #1904
  • Mar 23 00:13

    jaraco on master

    (compare)

  • Mar 23 00:13
    jaraco edited #1634
  • Mar 23 00:13
    jaraco edited #1760
  • Mar 23 00:13
    jaraco edited #1796
  • Mar 23 00:13
    jaraco edited #1834
  • Mar 23 00:13
    jaraco edited #1835
  • Mar 23 00:13
    jaraco edited #1894
  • Mar 23 00:13

    jaraco on main

    (compare)

  • Mar 21 20:40
    webknjaz commented #1193
  • Mar 21 20:13
    jaraco commented #1193
Sviatoslav Sydorenko
@webknjaz
João Pinto
@joaompinto
@webknjaz thanks
João Pinto
@joaompinto
I have read about a strategy about using a thread to manage db connections and provide them to the web handling threads via queues, does someone have some example code with something similar ? I am mostly curious if this can be integrated with the ceroot bus/thread manager.
Sviatoslav Sydorenko
@webknjaz
@joaompinto Cheroot does not have a bus concept, CherryPy does
João Pinto
@joaompinto
João Pinto
@joaompinto
reading it with more attention, it does not match entirely my requirement because the plugin runs in the calling thread, it is thread safe because of the sqlalchemy scoped_session. I guess for my case I will need to explicitly create a thread from the plugin to manage the connection requests
João Pinto
@joaompinto
I guess I need a "QueueMonitor" with a "QueueBackgroundTask"
ajyoung
@ajyoung
@webknjaz -- re: 210 -- I didn't follow your last comment. The test succeeds under python3, it doesn't fail. But on python 2.7, at least under CentOS 7.5.1804, the test suite doesn't run.
OH, sorry.... you're saying socket in python2 doesn't support CM while in python3, it does. Right?
Argh. I'll dig on that.
ajyoung
@ajyoung
Still don't know why on my system the tests don't run under python2.7 but I guess if I can get them to run on the CI server, that may be enough
Sviatoslav Sydorenko
@webknjaz
@ajyoung what exactly doesn't work for you locally?
ajyoung
@ajyoung
@webknjaz Some of the python 2.7 tests don't pass locally but that is ok since they pass on the CI. I can just run them on the CI and wait for those results. What is more concerning is that the test doesn't fail when run against master (without the fix for 210). I'll work on the changes to the exception type you suggested and then try again.
Sviatoslav Sydorenko
@webknjaz
@ajyoung some tests may be flaky. If not, this is totally worth exploring. At least until we drop py2
vcmorini
@vcmorini

@vcmorini I had great results in the past using CherryPys builtin http server in production, there is no silver answer to your question, the benefit of using multiple processes/multiple threads/reverse proxy, can depend a lot on the application workload

thks a lot!

doppelhund
@doppelhund-developer
hello guys, i am building an api using cherrypy and i use multiprocessing for heavy calculations. however every time a process or pool is being created i get the error "can't pickle SSLContext Object". i have checked my program code seperately without integrating it into cherrypy and it worked fine. it seems that there is a conflict of cherrypy and multiprocessing. does anybody know how i might fix htis?
Sviatoslav Sydorenko
@webknjaz
Sounds like you do something weird. When you use multiprocessing, a lot of things get pickled and copied into subprocesses, then unpicked. This is something it does under the hood.
Looks like you're attempting to do this after the server has been initialized and it doesn't make sense to me: why would you need multiple servers that are already bound to the same network interface in subprocesses.
doppelhund
@doppelhund-developer
maybe you understood me wrong, when a user makes an api call i need to do some heavy calculations and to make those faster i use multiprocessing
Sviatoslav Sydorenko
@webknjaz
FWIW I believe that you're passing some internal object related to the TLS connection into a subprocess and that can't be serialized.
doppelhund
@doppelhund-developer
It happens to what ever I pass, numbers, strings, dicts...
Sviatoslav Sydorenko
@webknjaz
I don't think so
Mori Bellamy
@moribellamy
i'm designing a webserver to run on Windows. looking at PyInstaller to distribute and cherrypy with flask as implementation. does that stack raise any red flags to anyone?
Jason R. Coombs
@jaraco
Thanks for the heads-up on the Github Sponsors. I appreciate the interest, but I’m not sure I’m ready to invest the time in applying for such an account. I will consider it, though.
Jesse Horne
@jessehorne
I have an API using cherrypy that another API we have uses quite a bit, so we end up having several requests that sometimes take up to 8 seconds happening in a minute. There's no way around that time for our usecase. One issue we're having is that there ends up being a ton of issues connecting to the API using cherrypy. We're not using sessions and everything is stateless. Is there some gotcha somewhere that would be using it up? We have 100 threads set in the config so I doubt its that.
Sviatoslav Sydorenko
@webknjaz
@jessehorne try stress-testing, maybe? Do you know if you really don't hit 100 requests simultaneously? Do you measure how much time your specific HTTP handlers consume? Can it be that it's one handler that does some heavy lifting? Any DB connections or anything that could make them slower? Cheroot/CherryPy versions? It would be useful if you could draft some code examples. Do you receive any sort of HTTP 500 or is it just slow? Is your HTTP request payload big or small? What about the HTTP response? Is your environment set to production in the config? Do you have any custom plugins or tools in your project? Do you have TLS enabled? Which one? How is it deployed? It's a thread-based server so you should always remember that GIL exists. You could have multiple instances in separate processes with external tooling that would load-balance the requests. All of these have potential to somehow influence your app (as well as your own code, of course).
Jesse Horne
@jessehorne
Thanks @webknjaz! This actually gives me a great heading.
obuno
@obuno

Hi all, let my 1st apologize for my lack of cherrypy knowledge. I can't seem to get my code working to satisfaction.
The goal is to serve a staticdir on "foo" and other very simple dynamic returns on a 2nd Virtual Host.
The issue I'm facing is that the staticdir hence static site isn't reached and I'm only able to get the "I am vhost: foo" on the mapped fqdn.

Here is my code:

import os
import cherrypy
from cherrypy import expose

class Root(object):
    @expose
    def index(self):
        return "Main vhost"

class foo(object):
    #pass
    @expose
    def index(self, param=None):
        return "I am vhost: foo"

class bar(object):
    @cherrypy.expose
    def index(self):
        return "I am vhost: bar"

def main():

    root = os.path.dirname(os.path.abspath(__file__))  # this file's directory

    cherrypy.config.update( {
        'server.ssl_module': 'builtin',
        'server.ssl_certificate': root + '/ssl/server.crt',
        'server.ssl_private_key': root + '/ssl/server.key',
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 443,
        'log.screen': True,
        'log.access_file': root + '/logs/access.log',
        'log.error_file': root + '/logs/error.log',
    } )

    conf = {
            '/': {
            'request.dispatch': cherrypy.dispatch.VirtualHost(
                    **{
                        "foo.fqdn.suffix": "/foo",
                        "bar.fqdn.suffix": "/bar"
                        }
                    )
                }
            }

    foo_conf = {
            '/foo': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': root + '/public/foo',
                'tools.staticdir.index': 'index.html'
                }
             }

    bar_conf = {
        '/bar': {
            }
        }

    root = Root()
    root.foo = foo()
    root.bar = bar()
    cherrypy.tree.mount(root, "/", conf)
    cherrypy.tree.mount(root.foo, "/foo", foo_conf)
    cherrypy.tree.mount(root.bar, "/bar", bar_conf)
    cherrypy.engine.start()
    cherrypy.engine.block()

if __name__ == "__main__":
    main()

Any help is much appreciated. Thanks!!

Sviatoslav Sydorenko
@webknjaz

@obuno that's alright. The VirtualHost code is quite old and I don't even think I ever used this feature. Let me try replicating your problem (probably without TLS because it's likely unrelated). I see a few unexpected patterns in your snippet (like multiple tree mounts) that are probably unnecessary and maybe interfere with the dispatching. Also, I don't know how the static tool interacts with the VirtualHost dispatcher, so I'll need to look into it too...

By the way, I've enabled GitHub Discussions in the repo (it's a feature that's virtually a clone of StackOverflow) — when you discover something unanswered like this, it's useful for the community to publicly publish the question and the solution so others could find it by googling. Posting it on either Discussions or StackOverflow would be great.

Aha! I see we have a test that specifically has the static tool and virtualhost configured together: https://github.com/cherrypy/cherrypy/blob/master/cherrypy/test/test_virtualhost.py. So you may want to follow that example.

At glance, it looks like you need to put the whole config in the first mount and drop the rest.

Sviatoslav Sydorenko
@webknjaz
And the static test also has virtualhost integrated: https://github.com/cherrypy/cherrypy/blob/master/cherrypy/test/test_static.py
Sviatoslav Sydorenko
@webknjaz
@obuno so you don't need a mountpoint for foo at all and also you can put the config for that path under the root one
This is what works:
from pathlib import Path

import cherrypy


PKG_DIR = Path(__file__).parent.resolve().absolute()  # this file's directory


foo_conf = {
    '/foo': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': str(PKG_DIR / 'public' / 'foo'),
        'tools.staticdir.index': 'index.html',
    }
 }


bar_conf = {
    '/bar': {
    }
}


conf = {
    '/': {
        'request.dispatch': cherrypy.dispatch.VirtualHost(
            **{
                'foo.fqdn.suffix': '/foo',
                'bar.fqdn.suffix': '/bar',
                }
            )
        },
    **foo_conf,
    **bar_conf,
}


class Root:
    @cherrypy.expose
    def index(self):
        return 'Main vhost'


class Bar:
    @cherrypy.expose
    def index(self):
        return 'I am vhost: bar'


root = Root()
root.bar = Bar()


def main():
    cherrypy.config.update({
        'log.screen': True,
        'log.access_file': str(PKG_DIR / 'logs' / 'access.log'),
        'log.error_file': str(PKG_DIR / 'logs' / 'error.log'),
    })

    cherrypy.tree.mount(root, '/', conf)
    cherrypy.engine.start()
    cherrypy.engine.block()


if __name__ == '__main__':
    main()
obuno
@obuno
@webknjaz Thanks a lot, indeed working like a charm !!! Again, thanks so much for your help!!
tithir
@tithir
hi, i am new with cherryPy, i have a question
Currently i am trying to use CherryPy to print a output of an existing Python script, but no values aer being shown
It would be helpful if someone can give some examples
Sviatoslav Sydorenko
@webknjaz
print where?
console?
CherryPy is an HTTP framework so the expected use is to send data in HTTP responses
tithir
@tithir
@webknjaz print the same in the front end
this is the code

import cherrypy
import os
import os.path

class PreecoolingDC(object):
@cherrypy.expose
def index(self):
return open('index.html')

@cherrypy.expose
def store(self,length=30):
    exc = exec(open('file.py').read())
    return "Hello %s" % exc                  

configfile = os.path.join(os.path.dirname(file),'server.conf')
cherrypy.quickstart(PreecoolingDC(),config=configfile)

Sviatoslav Sydorenko
@webknjaz
Well, this does send the HTTP response just fine (ignoring the fact that exec() is evil and must almost never be used)
tithir
@tithir
How should that be used if you can help with an example
Sviatoslav Sydorenko
@webknjaz
That is up to you
for example,
$ curl http://localhost:8080/store
Alexandre LICINIO
@alexandre.licinio_gitlab
you can also use mako to display from server to a webpage
from mako.lookup import TemplateLookup

@cherrypy.expose
    def index(self):
            template = lookup.get_template("index.html")
            return template.render(foo='bar')
in your html
<title>${foo}</title>