2

I am making a program that simulates a stock market for a virtual currency. I have not tried anything yet, but I want a Python script to run 24/7 online for a long period of time. The python script should be something like this:

import time
import random
  
while True:
    number = random.randint(0,5)
    print(number)
    time.sleep(2)

Also, a separate local Python program should be able to retrieve the number variable constantly every 2 seconds. I do not need a recommendation for a product or service. I just need to know what code I need to run and if I need a physical or web server.

If I use a web server, I will be paying monthly for a long time. This project needs to be running online for theoretically years (setting aside downtime and maintenance).

I have barely any experience in servers and networking, and I couldn't find answers online. Again, I have not tried anything yet.

Zer0 - Official
  • 35
  • 1
  • 2
  • 8
  • You can look into “flask” or “fastapi” to create a simple web server. Then, other processes can query for the number using the url address of the web server. – Tim Mar 09 '22 at 22:36
  • @Tim I did look into flask, and I cannot figure out how to make a local Python program access numbers that the online script generates. Can you please explain more? – Zer0 - Official Mar 09 '22 at 22:38
  • Do you want to actually be on the internet or just a network simulation. Sounds like you might want a simple docker compose solution with two containers – JonSG Mar 09 '22 at 22:43
  • @JonSG I actually want to be on the internet. The user will be on a local Python program that connects to the online one to get the same numbers as others. – Zer0 - Official Mar 09 '22 at 22:44

3 Answers3

2

There is a pretty good IDE replit.com where you can run your python but if you want to run it 24/7 you have to get the “hacker” plan which you can get for free with the github education pack if you connect it to your replit account, this pack is also free if you’re in college or high school with a student card. You can work around this by adding a keep alive script to your code. It has pretty fast server that have almost no downtime.

  • I do not want to get a "plan" for a service. I would prefer to get a long-term solution that will save me money in the long run. I am not concerned about downtime, but I want the same program to run for theoretically years (with the occasional power-outage or maintenance if needed). – Zer0 - Official Mar 09 '22 at 23:01
  • Yeah then you can just add a keep alive script to your code – bijenmanlol Mar 10 '22 at 23:03
1

If you really want to just simulate it and there is only one user. You can just return a random number each time the user make a query. It’s irrelevant how often they query it. You can put this up locally or on heroic free plan. But the fact that the user is querying every 2 seconds means lots of requests and so you may exceed the quota.

import random
from flask import Flask
import time

app = Flask(__name__)

@app.route("/")
def hello_world():
    return random.randint(0,5)

Say you up it up locally on port 5000. Then, simply going to “localhost:5000” via python or browser will give you that random number.

To serve it up to multiple people, you want to have a separate thread running to generate the number. Then, the view would return the number at the URL. For example,

from flask import Flask
import random
import threading

app = Flask(__name__)

number = 0

@app.route("/")
def hello_world():
    """ URL returns random number """
    return str(number)


def gen_rand():
    """ generating random numbers """
    global number
    while True:
        number = random.randint(0, 5)
        time.sleep(2)

if __name__ == '__main__':
    # starting thread to generate the number
    x = threading.Thread(target=gen_rand, daemon=True)
    x.start()

    # starting web server
    app.run()

By default, the local webserver will start at localhost:5000, go to this URL in your browser and you will see the randomly generated numbered. You can open multiple browser tabs to see they will give same random number if refreshed within 2 seconds.

Note that using global variable is not thread-safe or process safe. You should use database or redis to update and load “number”. See this question for further discussions: Are global variables thread-safe in Flask? How do I share data between requests?.

Tim
  • 3,178
  • 1
  • 13
  • 26
  • Yes, I would like, at most, 10 users. Yes again, even if no one is currently connected, the number should be changing always in the cloud. If (for example) 3 people are using the same local Python program and connect to the online script, they should see the same number changing (somewhat simultaneously) at the same rate and number. – Zer0 - Official Mar 09 '22 at 23:21
0

Sounds fun. Shouldn't cost you anything. The popular serverless options (Azure Functions, AWS Lambda etc.) give you 1M free invocations per month.
If I'm not mistaken, Google's Cloud Function is 2M.

  • That would be a good solution, but I am planning on running this process for a longer time period. Theoretically years. Can I use a physical server to produce the same result without paying for an online solution? – Zer0 - Official Mar 09 '22 at 22:49
  • I'd think that potentially having it run for years is an argument against a self-hosted environment. What is there's a storm and you lose internet etc.? Look into the options I mentioned, they are always free within the monthly tier which I think should be more than enough for this. – PythonPerfection Mar 09 '22 at 22:55
  • Well, I'm not talking about running non-stop for years. It can be down for a while sometimes just like any server, but I don't want to pay someone for a very long time for a server, if I can find a long-term solution that will save me money in the long run, that will be my best option. – Zer0 - Official Mar 09 '22 at 22:58