## Context
This is for beginners.
For when your first Python bot or script for Hive goes to production.
I am not the right guy to post about clean code, but I've learned a few things.
I've made some mistakes and will make more, but maybe I can help you avoid some.

## server ban
When querying from a public Hive node, that means using https.
The nodes are behind firewalls and other protection.
If you spam queries, you can get banned by these systems.
That's not cool for you and also it's friendlier to just avoid that altogether.
## loop
To infinitely loop a reoccuring job, like a daily compilation of votes or something, [cron](https://en.wikipedia.org/wiki/Cron) can be good. Or [schedule](https://schedule.readthedocs.io/en/stable/index.html). But even then: If you have to make a lot of calls at once, maybe better include time.sleep(2). At least don't blindly iterate over pages of results.
## 3 second loop
I'll be writing a lot more about calling for a new block every 3 seconds (block stream).
For now: it's pointless to do the query more than once every 3 seconds.
The main loop should idle for the rest of the time. (time.sleep(3 - (time.time() - start_time)))
## async vs. threads
While true: At the end it's just an infinite loop anyways, but [asyncio.loop.run_forever](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_forever) looks nicer.
If it's the first time you see co-routines: Don't worry.
Forget about [multiprocessing](https://stackoverflow.com/questions/27435284/multiprocessing-vs-multithreading-vs-asyncio), btw.
## signal interrupt
Using [an example](https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python), I came up with this:
```
import signal, time
user_interrupt = False
def interrupt_signal_handler(sig, frame):
global user_interrupt
user_interrupt = True
signal.signal(signal.SIGINT, interrupt_signal_handler)
def thing():
print("can't...")
time.sleep(1)
print("...interrupt me")
while user_interrupt == False:
thing()
```
At least you can break out of the loop somehow cleaner.
You can also use signal for [networking and interprocess communication](https://runebook.dev/en/articles/python/library/signal/signal.signal). Powerful.
## Linux
At the end, you'll most likely want to install it all on a server and that's when you'll find that Linux is best for that. At which point you might as well use Linux layer itself to handle task queues and processes, like I already mentioned, when I wrote about cron. And the link, one paragraph above, even demonstrates network sockets.
That's taking it a bit far though; Most bots can probably live in a single main loop, but keep scalability in mind at least.
Still ontopic: [terminal arguments](https://stackoverflow.com/questions/17544307/how-do-i-run-python-script-using-arguments-in-windows-command-line) could be useful.
## Server Security
Talking about remote servers and their security is not something I should be giving advice about.
Still better than nothing: **login via [ssh](https://ubuntu.com/server/docs/openssh-server)**, use [Fail2Ban](https://en.wikipedia.org/wiki/Fail2ban) and [ufw](https://ubuntu.com/server/docs/firewalls).
## Errors
Here's a good [video on error handling in Python](https://www.youtube.com/watch?v=8kTlzR4HhWo).
In short: don't make errors.
Whether in development or production: if it's a critical error, it should probably crash and you need to fix it. If the error isn't critical, and you can't avoid it, catch and and make it a warning.
## Storage
When your bot crashes, Hive probably keeps running.
When restarting, it can be important to know where it stopped.
Those problems can only be solved by storage and **forget writing your own storage system with file writing** and other such experiments.
[SQLAlchemy](https://www.sqlalchemy.org/) (especially sessions) can make stuff robust and scalable. And once you understand it, you already have a strong foundation to learn [flask](https://flask.palletsprojects.com/en/3.0.x/) next...
## Service
It can be fun in the beginning, but servicing bots and managing servers and whatever 24/7 can be a nightmare. So before you commit, think about long term, too.
It can eat up hours and breaks at the worst moments and causes stress.
If it turns into a job, it better be worth it.
## Conclusion
Don't be **that** guy.
Shit bots can cause trouble, so please be considerate.
Expecially when broadcasting: Once something was sent, it's out in the world (of blocks) forever.
That's all I could think of right now. I commented some place else that infinite loops are cringe, so I wanted to share some alternatives and considerations. In the links you should find helpful concepts to get you started on the right foot at least.
**Please** comment, if you want to add something or ask or whatever.
Archive post
Your 1st Python Hive Bot Deployment
beginner help