Forums

how to place variables in separate file

Hi, I'm a beginner, so I think is quite easy question, anyway...

I've got telegram bot script, which has logic using arrays and dictionaries. In few words - when user send me reply to certain question array with question updates. When questions finished, if user accept - I update database.

Here is a problem. In payed account "hacker" 2 workers available, so I have my script running on 2 machines, and I don't control which one get reply from user. As I have 2 machines, I have 2 sets of variables, which comes to a real mess. If user's reply start with worker 1 (machine 1) and in the middle his reply go to worker 2, I have total destroy of logic.

May be is possible to have separate file, where I can hold my variables, and both workers will connect to this file when needed?

many thanks

Normally the best way to do that kind of thing is to use a SQL database instead of a file; databases are very good about handling concurrent changes from different running processes, while with a file you have to be really careful and write complex code to make sure that a change from one process doesn't overwrite a change from another. You can use MySQL on PythonAnywhere -- it's included in your account.

Is your bot running as a Flask app? If so, you might find it useful to create a throwaway free account and use it to go through our Flask and MySQL tutorial to get a feel about how your code should interact with a database.

thank you for reply! Yes bot is using flask + mysql. I think I made a stupid logic using all these dictionaries instead of mysql. I just uploaded all info to mysql when it was collected (its collected after several replies from user), but now I see is better to do it even at intermediate steps with mysql. Thank you

OK -- let us know if you have any problems moving stuff to MySQL. If you need it up and running then of course we can switch you back to having just one worker process temporarily for a time, though that would mean that you can handle only half the traffic that you can with two workers.