My Heroku app seems to be running more than 1 instance - python

As the title says, I have an app hosted on Heroku that seems to have more than one instance running. This is a problem because I am trying to keep track of single letter posts on a GroupMe in order to see if they spell anything. I am using python and a global string variable that I can add the characters to. Since there is more than one instance, there is more than 1 global variable so sometimes the character gets added to one or the other which defeats the purpose of the program. Has anyone else run into this problem or found a fix to this? Thanks.

So after thinking about possible ways to fix it, I decided to try to keep my global variables inside a json file and then edit that each time. This worked as I believe that even if there were 2 version of the actual code running, they should both edit the same json file.

Related

How to update code on multiple servers at once

I'm relatively new to working with server instances so please excuse my limited knowledge. I have a python script that I run for 5 different people. Each person who I run it for, has a slightly different file (Ie: some names in the script are changed to work with their branding and name), and right now when I get a new client, I have to copy the script, and change each part that needs to be changed by hand, and then run that in my server (I'm running all 5 scripts on the same server). If I make any changes to my codebase, I need to change the script for each of my clients, go to the server and swap out the old versions by hands.
I'm expecting to have this grow to a couple hundred clients so this is just not a feasible method. Are there any products or methods I can use to fix this product, or atleast make it easier to update all the files at once?
Without looking at the specifics of the script it's really hard to say, but here are a couple of solutions that might work:
Pass the values that change as arguments to the script - Use something like argparse to capture the arguments into variables
Modify the script to run on a list of arguments - This is pretty easy to do - literally just add a for-loop around your entire script and iterate over the values for each of your clients.

readline() returns nothing when run from certain file

I've been working on a project lately which requires me to get a value stored in a text file. Simple task, right?
I have tried pretty much every stupid solution that uses readline() at some point, but when it's printed, there's nothing. Debug also tells me that it is empty.
Since I've experienced some inconsistencies with python already, I tried using the same function inside a different python file, and running it does exactly what it's supposed to do.
My current solution (not the best, but currently I just want it to run properly) is
count_file = "count.txt"
filehandle = open(count_file, 'r')
line = filehandle.readline()
print(line)
which works fine when used in a file called "test.py". When used in my "main.py", it returns nothing. And yes, I used it on the same context and temporarily deleted everything else from the main file while testing.
Does anyone have a clue what causes this? I could just paste the other stuff into the test file and rename it, but 1. that's annoying and 2. it'd be useful to know how to avoid this.
EDIT: I am not certain yet, but I think that the problem is caused by my IDE (am using the latest version of Pycharm Professional). I solved the problem by deleting and re-adding the Run/Debug Configuration.
Sometimes you will get return value as None if you do not specify the file path. please check the path/relative path of "count.txt" file with respect to "main.py" script.
First point: your file path is relative, so it will be resolved according to the current working directory, NOT depending on your script's location.
Second point: you read a single line, it can indeed be empty or only contains non-printable characters.
Third point: run your code without pycharm. Pycharm is probably fine when you really understand what it does behind your back, but if you don't you will indeed experiment some unexpected results. Just using the command line and a simple code editor is actually much simpler (though possibly not as productive) and less confusing.
And finally, don't forget you have an interactive Python shell. Quite handy for testing out things...

kwargs.get only pulls first letter

New to Python, and I find this bit rather confusing. I have a config file that is read into a kwarg. with values like:
students||student a|student b|student c
I see this populating properly into the kwarg when I debug, however, when I attempt to read the values, it will always only pull the first letter.
for student in kwargs.get('students'):
--student will come thru as 's'
this seems to be something specific to my machine, as this code is running in prod. I set my libraries the same as the server, but still having the issue. Anyone know why this might be occurring?
I am running on Python 3.6.5
Thanks

Dynamic code generation in Python - Update of Code

I have a python application ready to launch.
One thing keeps tickling my mind is that the application depends on formal API of several sites to get data.
If one site got their API changed, i have to change the code and ask users to reinstall the new application. It's a lot of work if several things hang on at a time.
I came across exec, which could execute a string like a code snippet.
So, if it works good, I can save the critical code parts sqlite3 table.
In case of any change, I can ask users to do an OTA update from inside the application which will just update the sqlite3 table and code would work as usual.
But just got a hammer, return not working inside exec(), just getting return outside function exception. I don't know what are all others things that will go not working if I use exec.
Working:
def func_dyn():
if 1==1:
return 1
else:
print('test')
if __name__ == '__main__':
func_dyn();
Not Working:
global code
code="""if 1==1:
return 1
else:
print('test')
"""
def func_dyn():
global code
exec(code)
if __name__ == '__main__':
func_dyn();
How to handle the return in exec if exec was inside a function / the way it should be formatted/handled?
Why i need to put the whole code of a function into exec?
since there were many functions like this, I can't store small,small parts which could make code unreadable. So i was thinking to put whole function into string.
Why i need return?
If exceptions arise, the function should return to the caller and execute next.
Even though exec works in the current context but it doesn't seem to work in the context of the running method, so return was not possible. A workaround is set a flag inside exec and handle return outside. But exec was not a good candidate for my requirement. Instead, I have decided to properly incorporate update functionality in my code via my own standalone updater or via frameworks like pyupdater, esky, etc.
I think your approach is very difficult to debug. What is the purpose of storing code in SQL anyway? why can't you just prepare an external API file and update that if needed? This can be simply imported by adding a cache folder to your Python path programmatically and will keep your code where it belongs: a .py file.

How to find out application is using object reference in cache?

I find it hard to explain my problem as the problem is not within the code itself. If you need more input to understand the following better, please let me know.
I'm using ArcGIS to call a bunch of python scripts, one after another. The scripts use the same class by importing it, or inherit from it. To initialize the class I often use the same name. I have no idea how the communication between ArcGIS and python is implemented. But I noticed that my scripts are not always behaving like they should. I suppose that the reference to the object still exists (even though the script the object was created with has finished) when I call the second script.
First of all: How can I make sure my suspicion is true?
Secondly, if this is the case: is it a good idea to destroy all references to all objects using del or __del__? What is the best way to do this?
Trace tracing your code and walking through it with a debugger? Quickest way to tell if it's accessing correct code. Python Debugger
If you read the documentation, what you want to do is use a break point to make sure code reaches that point in code.
This is the syntax with the debugger.
b(reak) [[filename:]lineno | function[, condition]]

Categories