We use psycopg2 package to write ad-hoc jobs in Python. But this can lead to serious problem in production environments.
I have seen this couple of times in the last five years. When we write adhoc Python scripts using Psycopg2 without autocommit=True, you may see wierd spikes on your idle on transaction like below. This was a batch job. It was running every 10 seconds. If your batch job is not closing connections or restarting from the scratch, your SELECT queries might see very old versions of the data. We have to be very careful with this. As it can cause data loss too.
I also have seen idle_in_transaction_max_time goes to days in one instance too. That will stop auto-vaccuum doing its work. It will cause a lot of issues and slow down your database. When you use Psycopg, Make sure you have autocommit=True or you commit manually. You can see the warning on the documentation too.
Turns out the first function has more bytecode operations. It loads the None value and return it. I wanted to run some tests around the running time. I ran the function for thousands of times and recorded the running time.
Above two figures shows the difference between function_1 and function_2 execution time on an AMD Dual core processors. I was hoping to see more clear evidence, I thought function_2 would be slightly faster. X axis shows number of times I ran the functions in a loop. Y axis shows the difference in time (function_1 time – function_2 time).
Above two charts shows the same test on an Intel chip. I don’t see any clear evidence.
I ran the same number of functions calls for 250 times and got this variation in differences. I am still not sure about the results. I will investigate this further.
I bought a Hisense TV recently. I used it to watch a couple of downloaded movies. After a couple of weeks, the same files stopped working. TV showed an error message. I tried to find help online. It was not that helpful. I found out that clearing the cache fixed the issue.
I wanted to send emails with Twillio SendGrid. It offers a generous free tier. Set up the domain and authenticate the identity. You have to generate an API key with “Mail Send” privileges.
Set up your environment variables. My script will pick up credentials from the environment variables.
import os
import smtplib
from email.message import EmailMessage
def send_mail(
subject,
body,
from_address,
to_address,
cc_address=[],
file_list=[],
message_id="",
):
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = from_address
msg["To"] = to_address
if cc_address != []:
msg["Cc"] = cc_address
if message_id != "":
msg["In-Reply-To"] = message_id
msg["References"] = message_id
msg["Bcc"] = from_address
msg.set_content(body)
for file_path in file_list:
with open(file_path, "rb") as file_reader:
file_data = file_reader.read()
msg.add_attachment(
file_data,
maintype="application",
subtype="octet-stream",
filename=os.path.basename(file_path),
)
smtp_user = os.environ.get("SMTP_USER")
smtp_password = os.environ.get("SMTP_PASSWORD")
smtp_server = os.environ.get("SMTP_SERVER")
smtp_port = os.environ.get("SMTP_PORT")
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(smtp_user, smtp_password)
server.send_message(msg)
Now we can simply send an email like this.
from email.utils import formataddr
send_mail(
subject="Planet Express order status",
body="We have to deliver a package!",
from_address= formataddr(('Fry', 'fry@futurama.com')),
to_address=[formataddr(('Leela', 'leela@futurama.com'))]
)
I am going to try out a new blog format. I have tried to use WordPress, Blogger, and Github pages for my blog. One way or the other, I faced issues with every platform I tried.
I am going to use this repository to keep notes of my tests and see how it goes.
Recently, I registered a private limited company in Sri Lanka. My experience was surprisingly smooth. I could do all the paperwork online. Almost all the documents are generated via the online system called EROC. Payments can be done by cards too.
They have given a detailed guide too. But drafting the article of association wasn’t that straightforward. Some guidelines can be found here.
I prepared a template for anyone else to use. It can be downloaded from here
Make sure you leave enough free space on the first page of the article of association, officers want to add few lines of system-generated information. Maximum file size allowed is 4MB.