0

I'm building a simple test web application that stores info users enter in a mongo database.

No errors come up, but I always get the following warning (Im using django) when using the client.

/home/<User>/.local/share/virtualenvs/<Name>/lib/python3.7/site-packages/pymongo/topology.py:155: UserWarning: MongoClient opened before fork. Create MongoClient only after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#is-pymongo-fork-safe                                                       
  "MongoClient opened before fork. Create MongoClient only "

After checking out the API docs, it seems that the warning pops up when using fork() which im not explicitly using.

Maybe something that I'm doing is actually triggering the fork.

I create my mongo client at __init__ and then import the variable to my mongoDB handler. Then, that client that I imported is used inserting or updating items in the DB.

What is the correct way of creating and then using a mongo client to prevent this warning from appearing?

Manuel
  • 730
  • 7
  • 25
  • 1
    Is this the same issue? https://stackoverflow.com/questions/51139089/mongoclient-opened-before-fork-create-mongoclient-only-flask – Joe Apr 21 '20 at 18:35

1 Answers1

2

After reading the link Joe provided, ensure you are not performing database operations in global scope which is the most common way I can think of to get a client instantiated prior to a fork.

You are probably using a web server that forks under the hood, or some sort of reloader for development that forks to reset state.

D. SM
  • 13,584
  • 3
  • 12
  • 21