Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 14:22
    Kludex commented #2907
  • 14:09
    hesummerzh commented #2907
  • 13:51
    snow6oy commented #911
  • 13:10
    Suyoung789 edited #2908
  • 13:09
    github-actions[bot] commented #2908
  • 13:08
    codecov[bot] commented #2908
  • 13:08
    codecov[bot] commented #2908
  • 13:06
    codecov[bot] commented #2908
  • 13:06
    codecov[bot] commented #2908
  • 13:06
    Suyoung789 synchronize #2908
  • 10:03
    github-actions[bot] commented #2908
  • 10:01
    codecov[bot] commented #2908
  • 10:01
    codecov[bot] commented #2908
  • 10:00
    codecov[bot] commented #2908
  • 10:00
    codecov[bot] commented #2908
  • 10:00
    codecov[bot] commented #2908
  • 10:00
    codecov[bot] commented #2908
  • 10:00
    Suyoung789 synchronize #2908
  • 09:58
    codecov[bot] commented #2908
  • 09:58
    codecov[bot] commented #2908
David Montague
@dmontagu
if you subclass and modify the render method to set indent=4 and separators=(", ", ": ") in json.dumps I think it will render closer to the "pretty" way
something like this:
import json
import typing

from starlette.responses import Response

class PrettyJSONResponse(Response):
    media_type = "application/json"

    def render(self, content: typing.Any) -> bytes:
        return json.dumps(
            content,
            ensure_ascii=False,
            allow_nan=False,
            indent=4,
            separators=(", ", ": "),
        ).encode("utf-8")
then using it would look like
    @app.get("/", response_class=PrettyJSONResponse)
    async def get_some_json():
        ...
David Montague
@dmontagu
This might be useful for debugging. If you wanted it on all endpoints during development (and off in production), this could be done by dynamically setting the response_class to a variable depending on the environment. But I think you'd be better served just making use of dev tools that will parse and display the json more neatly for you (e.g., the swagger docs, chrome extensions, etc.), and just occasionally making temporary use of the PrettyJSONResponse in situations where it is temporarily useful to have it pretty-printed
@nrshapiro As @wshayes said, there is a built-in way to skip default values as long as you don't explicitly set them to the default value when instantianting the model (pydantic must have skipped setting the values). So if the default value is None, and you don't pass it to the constructor, it will be skipped during serialization. (It was designed this way, rather than just skipping nulls, because otherwise, if you want it to include None for some reason, (e.g., because you updated a non-null value to become null, and want to signal that in your response), you wouldn't be able to.)
William Hayes
@wshayes
@dmontagu Thanks for that example - I added it to my gists for future reference!
euri10
@euri10
added flower and celery to my base, flower is super nicely made
William Hayes
@wshayes
do you use rabbitmq or redis with celery?
euri10
@euri10
I put rabbitmq but tbh I d'm not sure if I get the pros and cons of each one\
betaalpha
@betaalpha_gitlab
@wshayes I have just started using redis+celery
William Hayes
@wshayes
I’ve been using celery/rabbitmq - just finding rabbitmq more difficult to administer than I’d hoped. How is your experience with redis/celery?
I’m just now in the process of implementing a really simple queue/background process using Redis RPOPLPUSH directly to see it that will simplify my services/life/etc. Just finding Celery/… more complex than I need.
betaalpha
@betaalpha_gitlab
I have not yet experimented at a large scale. Presently it is in Proof-of-Concept stage.

var code="@app.get("/long_task", status_code=202) # 202=Accepted
async def long_task():
from backend.workers import tasks
mytask = tasks.long_task.apply_async(kwargs={'num':30})
return {'task_id': mytask.task_id}

@app.get("/long_task/status/{task_id}", status_code=200) # 200=OK
async def long_task_status(task_id: str):
from backend.workers import tasks
result = tasks.long_task.AsyncResult(task_id)
return {'status': result.status}
"

Sorry, new to giiter. How to send code part?
:)
euri10
@euri10
3 backticks
betaalpha
@betaalpha_gitlab

``` @app.get("/long_task", status_code=202) # 202=Accepted
async def long_task():
from backend.workers import tasks
mytask = tasks.long_task.apply_async(kwargs={'num':30})
return {'task_id': mytask.task_id}

@app.get("/long_task/status/{task_id}", status_code=200) # 200=OK
async def long_task_status(task_id: str):
from backend.workers import tasks
result = tasks.long_task.AsyncResult(task_id)
return {'status': result.status}
```

euri10
@euri10
code
betaalpha
@betaalpha_gitlab
code
euri10
@euri10
you can switch to compose mode icon on the right to do it easily
betaalpha
@betaalpha_gitlab
code
That black background thing is still not coming. I am in compose mode. Sending using Cntrl+Enter
William Hayes
@wshayes
Info on adding code samples and using compose mode
They don’t make it easy
You can also edit prior messages
betaalpha
@betaalpha_gitlab
I have to be in compose mode. Write the following: var code=<Copy/Paste my code>; and press Cntrl+Enter
Is it?

```var code="@app.get("/long_task", status_code=202) # 202=Accepted
async def long_task():
from backend.workers import tasks
mytask = tasks.long_task.apply_async(kwargs={'num':30})
return {'task_id': mytask.task_id}

@app.get("/long_task/status/{task_id}", status_code=200) # 200=OK
async def long_task_status(task_id: str):
from backend.workers import tasks
result = tasks.long_task.AsyncResult(task_id)
return {'status': result.status}";```

Damn. I am looking foolish...
euri10
@euri10
you miss return carriage after the backticks
William Hayes
@wshayes
backticks have to be on separate line
betaalpha
@betaalpha_gitlab
var code="@app.get("/long_task", status_code=202)  # 202=Accepted
async def long_task():
    from backend.workers import tasks
    mytask = tasks.long_task.apply_async(kwargs={'num':30})
    return {'task_id': mytask.task_id}

@app.get("/long_task/status/{task_id}", status_code=200)  # 200=OK
async def long_task_status(task_id: str):
    from backend.workers import tasks
    result = tasks.long_task.AsyncResult(task_id)
    return {'status': result.status}"
Wonderful. Thank for bearing with me.
Following goes into celery_common.py
var code="from celery import Celery

CELERY_TASK_LIST = [
    'worker.tasks',
]

def create_celery_app():
    celery_app = Celery('worker')
    celery_app.conf.update(
        result_backend = 'redis://localhost:6379/1',
        broker_url = 'redis://localhost:6379/0',
        broker_transport_options = {'confirm_publish': True},
        task_track_started = True,
        task_ignore_result = False,
        accept_content = ['json'],
        task_serializer = 'json',
        result_serializer = 'json',
        worker_send_task_events = True,
        timezone = 'Asia/Calcutta',
    )
    return celery_app

celery_app = create_celery_app()
And in my workers
var code="from backend.celery_common import celery_app"
Thats all where I am now.
William Hayes
@wshayes
Thanks for sharing - var code … maybe a little javascript mixed in?
euri10
@euri10
does anyone know if flower is supposed to monitor beat tasks from celery ?
William Hayes
@wshayes
Haven’t tried that yet. It works pretty well for regular tasks - haven’t tried it for scheduled tasks.
euri10
@euri10
well seems like flower dont get them
euri10
@euri10

well seems like flower dont get them

I stand corrected, was triggering taks on stagging area and looking flower in local dev :confused:

betaalpha
@betaalpha_gitlab
from backend.celery_common import celery_app
@wshayes Just a python file for celery workers. They needs a common instance of celery
I decorate my celery tasks with @celery_app ...