I have a web app hosted on PythonAnywhere that uses Flask and PyMongo 4.6.1. It's been working fine for months and still works fine when I run it on localhost from my development machine. Connection to Atlas also seems good when I run it from PythonAnywhere console, but in the last few days I've been getting critical errors from the PythonAnywhere webserver even though I haven't touched any code that deals with the database connection:
No replica set members match selector Primary(),
Timeout: 30s, Topology Description: topology_type: ReplicaSetNoPrimary, servers: [ 27017) server_type: RSSecondary, rtt: 0.026010908652096987>, (ac-ptjy9js-shard-00-01.emgqo8z.mongodb.net, 27017) server_type: Unknown,
rtt: None, error=AutoReconnect(ac-ptjy9js-shard-00-01.emgqo8z.mongodb.net:27017:
connection pool paused)>, 27017) server_type: RSSecondary, rtt: 0.026285539381206036>]>
This test code works fine in the console or as a regular .py script:
client = MongoClient(
MONGO_URI,
connectTimeoutMS=30000,
socketTimeoutMS=None,
connect=False,
maxPoolsize=1,
)
class Test():
collection = client.HMO['Application Index']
@classmethod
def list(cls, query=None, projection=None):
if query is None:
query = {}
if projection is None:
projection = {}
return list(cls.collection.find(query, projection))
results = Test.list({'reference': '00631/6-8/P4'})
... but not inside Flask on PythonAnywhere (works fine on development machine and localhost):
@app.route("/test", methods=['GET', 'POST'])
def test():
response = Test.list({'reference': '00631/6-8/P4'})[0]
return str(response)
I've subsequently added settings recommended here https://help.pythonanywhere.com/pages/MongoDB/ but still not working. Is there anything else I need to do?
This is a critical error affecting a production system. Many Thanks for any help!
[edit by admin: formatting]