Skip to content

Commit

Permalink
Merge pull request #4 from Undertone0809/test-add-example2
Browse files Browse the repository at this point in the history
feat: change enable_mutithread to enable_async
  • Loading branch information
Zeeland authored Nov 22, 2022
2 parents a1ed380 + f0e89f9 commit 678c0ea
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# broadcast-service

broadcast-service is a lightweight python broadcast library. You can easily construct a broadcast pattern through this library.

## Setup

```sh
pip install broadcast-service
```


## Usage

There is a easy demo to show how to use broadcast-service.

```python
from broadcast_service import broadcast_service

Expand All @@ -30,15 +26,44 @@ if __name__ == '__main__':

```

You can see more example in [example](/example).
You can also add more params or no params when you publish thr broadcast.
```python
from broadcast_service import broadcast_service

## TODO
def handle_msg(info, info2):
print(info)
print(info2)

if __name__ == '__main__':
info = 'This is very important msg'
info2 = 'This is also a very important msg.'

# listen topic
broadcast_service.listen('Test', handle_msg)

# publish broadcast
broadcast_service.broadcast('Test', info, info2)
```
```python
from broadcast_service import broadcast_service

def handle_msg():
print('handle_msg callback')

if __name__ == '__main__':
# listen topic
broadcast_service.listen('Test', handle_msg)

# publish broadcast
broadcast_service.broadcast('Test')
```
Actually, you can see more example in [example](/example).

## TODO
- optimize documents and show more examples.
- optimize the syntax expression of broadcast-service
- provide more test cases


## Contribution

If you want to contribute to this project, you can submit pr or issue. I am glad to see more people involved and optimize it.
4 changes: 2 additions & 2 deletions broadcast_service/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self):
"""
self.topic_list = []
self.subscribe_info = {}
self.enable_multithread = True
self.enable_async = True
self.thread_pool = ThreadPoolExecutor(max_workers=5)

def listen(self, topic_name, callback):
Expand All @@ -84,7 +84,7 @@ def broadcast(self, topic_name, *args, **kwargs):
return

for item in self.subscribe_info[topic_name]:
if self.enable_multithread:
if self.enable_async:
self.thread_pool.submit(item['callback_function'], *args, **kwargs)
else:
item['callback_function'](*args, **kwargs)
Expand Down
29 changes: 29 additions & 0 deletions example/demo2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2022 Zeeland(https://github.com/Undertone0809/). All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from broadcast_service import broadcast_service

def handle_msg(info, info2):
print(info)
print(info2)

if __name__ == '__main__':
info = 'This is very important msg'
info2 = 'This is also a very important msg.'

# listen topic
broadcast_service.listen('Test', handle_msg)

# publish broadcast
broadcast_service.broadcast('Test', info, info2)
25 changes: 25 additions & 0 deletions example/demo3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 Zeeland(https://github.com/Undertone0809/). All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from broadcast_service import broadcast_service

def handle_msg():
print('handle_msg callback')

if __name__ == '__main__':
# listen topic
broadcast_service.listen('Test', handle_msg)

# publish broadcast
broadcast_service.broadcast('Test')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setuptools.setup(
name="broadcast_service",
version="1.1.5",
version="1.1.6",
author="Zeeland",
author_email="[email protected]",
description="A lightweight third-party broadcast library",
Expand Down

0 comments on commit 678c0ea

Please sign in to comment.