What goes wrong when you run it on your own PC
When you first write a Python script, it is something you run when you need it. But after using it for a while, you always end up thinking: I wish this just ran by itself.
Fetching data every morning. Checking stock every hour. Collecting notifications and adding them up. None of these needs a person at the desk. Yet on your PC:
- closing the PC stops it, and it does not run while you are out
- an update restarts the PC, and by morning it has stopped
- you cannot leave in the middle of a long job
- leaving it on costs a few hundred to about a thousand yen a month in electricity
Move it somewhere else and all of that goes away. And ¥30 a month is usually cheaper than the electricity for leaving a PC on.
How much does it need?
For Python scripts, what matters is almost always memory, and that depends on how much data you load.
| What you want to do | Memory needed | Suitable plan |
|---|---|---|
| Call an API and save the result | up to 128MB | Mini (256MB) |
| Fetch pages and extract their content | 128–300MB | Basic (512MB) |
| Process tables with tens of thousands of rows | 300MB–1GB | Plus (1GB) / Memory Basic |
| Load a large table all at once | 1GB and up | Memory Plus (2GB) or larger |
Libraries such as pandas that load a whole table into memory need a lot more at once. Plan for three to five times the size of the file you load and you will not go wrong.
Standard or High memory?
At about the same price, Standard plans get more CPU and High-memory plans get more memory.
- The computation itself is heavy (conversion, aggregation, lots of loops): Standard
- You keep data in memory (large tables, caches): High memory
Running at set times (scheduled jobs)
Schedules like "once at 7:00 every morning" or "every 10 minutes" are written in cron format: five fields.
# min hour day month weekday
0 7 * * * once a day at 7:00
*/10 * * * * every 10 minutes
0 */3 * * * every 3 hours
0 9 * * 1 every Monday at 9:00
You could also wait inside the script with while True: and time.sleep(), but for set times, a scheduled job is more reliable. The program exits when the job is done, so it does not keep holding memory.
On the other hand, things that should always be running (waiting for incoming messages, watching something constantly) should run as an always-on program, not a scheduled job. If they crash, they are restarted automatically.
From uploading to running
- Add balance. Credit cards, PayPay, bank transfer, Apple Pay and Google Pay all work.
- Choose a plan and a period, and buy it. It is ready the moment you buy.
- Upload your files. Upload them from your browser, or write them right there. No SSH needed.
- Install dependencies and start it. With a
requirements.txtin place, the libraries you need are installed together.
Always include requirements.txt
What you installed with pip install on your PC is not on the server. Write it out like this and upload it with your code.
pip freeze > requirements.txt
Keep secrets out of your code
Do not write API keys or passwords into your code; read them from environment variables. You can set these in the panel.
import os
API_KEY = os.environ["API_KEY"]
Keeping it from stopping
An always-on program will crash sooner or later. The other server did not answer, unexpected data came in, the connection dropped. These things happen all the time.
ASHIKA Network detects crashes and restarts the program automatically, so it will not sit there stopped until morning. On top of that, taking care of two things in your script makes it much more stable:
- Wrap anything that talks to the outside in
try, so one failure does not stop everything. - Write progress to a file or database, so it can carry on from where it left off after a restart.
import time
while True:
try:
do_work()
except Exception as e:
print("Failed:", e)
time.sleep(60)
Reading the logs
Whatever you print() streams straight into your browser. That is enough to see whether it is running.
For things that run for a long time, print the time along with each message so you can trace what happened later.
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
)
logging.info("Starting")
Common stumbling blocks
ModuleNotFoundError
A library is missing. Upload requirements.txt and install again. What you installed on your PC does not come along by itself.
Garbled text
Specify the encoding when you open a file. Without it, the way the file is read depends on the environment.
open("data.csv", encoding="utf-8")
Times are off by several hours
The server clock may run on UTC. If you want a specific time zone, set it explicitly. For Japan time:
from datetime import datetime, timezone, timedelta
JST = timezone(timedelta(hours=9))
print(datetime.now(JST))
It crashes partway through
Suspect a lack of memory. Either process large files a bit at a time instead of loading them all at once, or move up one plan.
