0

I am running a Python script (version 3.6.8) on AWS t2.micro instance with Amazon Linux AMI 2018.03. The script runs two long-running threads as shown below.

I monitor CPU and memory usage with htop, which shows 2 processes for my script both taking resources. Based on the output it doesn't seem like two processes are actually running. On macOS, where I developed the script, htop shows only 1 process for my script. The first image below shows collapsed processes on Linux and the second one shows them in a tree view.

Am I using Python threads the wrong way? Or do macOS and Linux show processes for python script differently?

from threading import Thread
from time import sleep


def thread(sec_sleep):
    while 1:
        print('sleep', sec_sleep)
        sleep(sec_sleep)


def init_services():
    t = Thread(target=thread, args=(1, ))
    t.daemon = True
    t.start()


def main():
    init_services()
    thread(2)


if __name__ == "__main__":
    main()

enter image description here enter image description here

1
  • output looks fine to me ... what do you mean when you say the output makes it look like they are not both running ... Commented Jan 9, 2020 at 5:29

1 Answer 1

3

First of all there is nothing wrong with that. htop shows individual threads as separate processes by default. If you are press capital H it will show only main process(press again to switch back).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.