question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FastAPI seems much slower that jupyter by vscode when useing pymongo

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@appmat.post("/mt")
def mtest(num: int):
    s = time.perf_counter()
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    db = client['tdm']
    mycollec2 = db['ttt']
    test2Data = []
    for i in range(num):
        test2Data.append({
            "time": i,
            "a": random.randint(1, 8),
            "b": random.randint(0, 1)
        })

    mycollec2.insert_many(test2Data)

    e = time.perf_counter()

    return str(round(sys.getsizeof(test2Data) / 1024 / 1024, 2)) + "MB", e - s

Description

Thanks for help. I am a learner in Python and FastAPI.My work is very simple,using FastAPI to insert data to mongodb by pymongo.

  • pymongo version : 4.2.0.
  • mongodb version:MongoDB 4.2.8 Community Standalone
  • FastAPI Start Command:uvicorn.run('main:app', host='127.0.0.1', port=58000, reload=True)
  • CPU:AMD 5800H
  • RAM:32G

My problem is that I found inserting data using the way I mentioned above was slow ,but when I copy the code and run in vscode using jupyter directly,it’s much faster than before You can see the detail in the below table:

num fastapi jupyter
1000 0.06569519999902695 0.017709599998852354
10000 0.23583729999882053 0.10930559999906109
100000 2.2969444999998814 1.0585518999996566
1000000 23.430175199999212 9.922307700000601
10000000 238.93187810000018 98.82192099999884

So did I do something wrong or how could I make FastAPI running faster when inserting data in my project?

Operating System

Windows

Operating System Details

Window11 22622.598

FastAPI Version

0.85.0

Python Version

Python 3.10.7

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MintBluecommented, Sep 21, 2022

Sorry for taking everyone’s time,it’s my mistake.I run the code in debugger mode in pycharm. I have not realized that the debugger mode would cause performance lost so much before when using python. And when I running the exe packaged by pyinstall,the cost time is close to jupyter.

Thanks again

1reaction
ThirVondukrcommented, Sep 21, 2022

I think in this case there should be no difference between running code in a fastapi endpoint and running the code as-is 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

New version of Jupyter notebook is extremely slow · Issue #7069
Recently I updated my jupyter notebook in vscode. I''m glad that many jupyter features are supported in the nee version. However, I found...
Read more >
vscode ipython interactive kernel slow startup and execution
Solution: It is recommended that you create or open a python file ( .py file) after opening VS Code, wait for the extension...
Read more >
Editing Python in Visual Studio Code
Pylance seems slow or is consuming too much memory when working on a large workspace. If there are subfolders you know can be...
Read more >
Time Series with Python & MongoDB Guide - Blog Post
In this blog post, we're going to uncover how to use Time Series data with Python and MongoDB. If you want to learn...
Read more >
sitemap_2.xml - MongoDB
... 2020-11-11T09:31:01Z https://www.mongodb.com/community/forums/t/using-linq-to-pass-iqueryable-document-into-a-constructor-via-select/11508 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found