An open API service indexing awesome lists of open source software.

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

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:

![lifespan result screenshot](static/lifespan.jpg)

## 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")

```