https://github.com/geeksloth/lifespan-multithreading
Simple Python multithreading examples
https://github.com/geeksloth/lifespan-multithreading
multi-processing multi-threading multiprocessing multithreading python3
Last synced: 10 months ago
JSON representation
Simple Python multithreading examples
- Host: GitHub
- URL: https://github.com/geeksloth/lifespan-multithreading
- Owner: geeksloth
- Created: 2023-07-10T12:04:03.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-11T14:24:12.000Z (about 1 year ago)
- Last Synced: 2025-01-30T14:48:46.120Z (12 months ago)
- Topics: multi-processing, multi-threading, multiprocessing, multithreading, python3
- Language: Python
- Homepage:
- Size: 162 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lifespan: Multithreading template
A simple example of Multithreading programming. This is a template for ones who want to start programming multithreading in Python.
`lifespan.py` result screenshot:

## Simple example implementation
```python
from threading import Thread
from time import sleep, process_time, time
class Life (Thread):
def __init__(self, name, lifespan):
Thread.__init__(self)
self.name = name
self.lifespan = lifespan
self.age = 0
def run(self):
print(self.name + " born")
while self.age <= self.lifespan:
sleep(1)
print("%s: %s" % (self.name, self.age))
self.age += 1
print(self.name + " died")
planet = [
Life("Mosquito", 1), #7
Life("Housefly", 4), #28
Life("Butterfly", 2) #14
]
if __name__ == "__main__":
tic = process_time()
for life in planet:
life.start()
for life in planet:
life.join()
toc = process_time()
print("main process time: {}".format(toc-tic))
print("Lifes are dead")
```