https://github.com/francois-le-ko4la/python-multithreading
Multithreading with Python
https://github.com/francois-le-ko4la/python-multithreading
multithreading python-3
Last synced: 10 months ago
JSON representation
Multithreading with Python
- Host: GitHub
- URL: https://github.com/francois-le-ko4la/python-multithreading
- Owner: francois-le-ko4la
- License: gpl-3.0
- Created: 2018-05-22T09:37:25.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2024-04-07T18:48:17.000Z (about 2 years ago)
- Last Synced: 2025-03-23T06:13:27.482Z (about 1 year ago)
- Topics: multithreading, python-3
- Language: Python
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pthread
## Description:
This package provide a simple way to manage multithreading in python project.
## Why:
- Clean the main code
- Simplify multithreading use with general objectives
## Why we will never use it:
- :x: All the code use multithreading
- :x: We use complex mecanisme between object: sync between object or data issue
## Setup:
```shell
git clone https://github.com/francois-le-ko4la/python-multithreading.git
cd python-multithreading
make install
```
## Test:
```shell
make test
```
## Use:
Take a look in the dev part.
## Project Structure
```
.
├── last_check.log
├── LICENSE
├── Makefile
├── pictures
│ ├── classes_pythread.png
│ └── packages_pythread.png
├── pythread
│ ├── __about__.py
│ ├── __init__.py
│ └── thread.py
├── README.md
├── runtime.txt
├── setup.cfg
├── setup.py
└── tests
├── test_doctest.py
└── test_pycodestyle.py
```
## Todo:
- [X] Create the project
- [X] Write code and tests
- [X] Test installation and requirements (setup.py and/or Makefile)
- [X] Test code
- [X] Validate features
- [X] Write Doc/stringdoc
- [X] Run PEP8 validation
- [X] Clean & last check
- [X] Release
- [X] change (un)install process
- [X] remove MANIFEST.in
- [X] manage global var: __version__....
- [X] improve the doc
- [X] Release : 0.1.1
- [X] improve Makefile
- [X] Release : 0.1.2
- [X] validate (un)install process
- [X] rename module : pthread => pythread
- [X] Release : 1.0.0
- [X] fix setup
- [X] improve docstring
- [X] update doc
- [X] Release : 1.1.0
## License
This package is distributed under the [GPLv3 license](./LICENSE)
### Runtime
```
python-3.6.x
```
### UML Diagram

### Objects
[PThread()](#pthread)
[@Property PThread.func](#property-pthreadfunc)
[PThread.start()](#pthreadstart)
[PThread.run()](#pthreadrun)
[PThread.stop()](#pthreadstop)
#### PThread()
```python
class PThread(Thread):
```
```
A class that represents a thread of control.
This class subclassed Thread class :
class Thread(builtins.object)
We specify the activity by passing a callable object to the constructor.
Why:
- clean the main code
- dedicate import Thread
- simplify multithreading use with generic objectives
Why we will neve use it:
- all the code use multithreading
- we use complex mecanisme between object: sync between object or
data issue
+----------+
| | define with func & elapse
--> INIT +-------------------+----------------------+
| | | |
+----------+ | |
| |
+----------------------------------------------+ |
| | | |
| +-----------------------------------------+
| | RUN | | | |
+----v-----+ | +----v-----+ +---+-v---+ |
| | | | | | | |
--> START +--------------> TASK +----------> TIMER | |
| | | | | | | |
+----------+ | +----^-----+ +----^----+ |
| | | |
+-----------------------------------------+
| |
+----------+ disable | |
| +-------------------+ cancel |
--> STOP +-----------------------------------------+
| +--------------------+
+----------+ |
+-----v-----+
| |
|JOIN Thread|
| |
+-----------+
Use:
>>> # Import the module :
>>> from pythread import PThread
>>> import time
>>> # define a task
>>> def mytask(): print("lorem ipsum dolor sit amet consectetur")
>>> # We want to run "mytask" in a thread and repeat the task:
>>> mthr = PThread(mytask, 0.1)
>>> mthr.start() ; print("other task");time.sleep(0.3) ; mthr.stop()
lorem ipsum dolor sit amet consectetur
other task
lorem ipsum dolor sit amet consectetur
lorem ipsum dolor sit amet consectetur
>>> # We want to run "mytask" in a thread one time:
>>> mthr = PThread(mytask).start() ; print("other task")
lorem ipsum dolor sit amet consectetur
other task
>>> # oups - start issue:
>>> mthr = PThread(mytask, 0.1)
>>> mthr.start() ; mthr.start()
Traceback (most recent call last):
...
RuntimeError: threads can only be started once
>>> # Stop a not repeatable task:
>>> mthr = PThread(mytask)
>>> mthr.start() ; print("other task")
lorem ipsum dolor sit amet consectetur
other task
>>> mthr.stop()
>>> # a test avoid the AttributeError exception
```
##### @Property PThread.func
```python
@property
def PThread.func(self):
```
>
> Returns the callable object defined by Thread constructor.
>
> Args:
> None.
>
> Returns:
> callable object
>
##### PThread.start()
```python
def PThread.start(self):
```
>
> Start the thread's activity.
>
> It must be called at most once per thread object. It arranges for the
> object's run() method to be invoked in a separate thread of control.
>
> This method will raise a RuntimeError if called more than once on the
> same thread object.
>
> Args:
> None.
>
> Return:
> None.
>
##### PThread.run()
```python
def PThread.run(self):
```
>
> Method (override) representing the thread's activity.
> This method will raise a RuntimeError if called more than once on the
> same thread object.
>
> Args:
> None.
>
> Returns:
> None.
>
##### PThread.stop()
```python
def PThread.stop(self):
```
>
> Wait until the thread terminates.
> This blocks the calling thread until the thread whose join() method is
> called terminates -- either normally or through an unhandled exception.
>
> Args:
> None.
>
> Returns:
> None.
>