goroutine-py 1.0.8__tar.gz → 2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- goroutine_py-2.0/PKG-INFO +116 -0
- goroutine_py-2.0/README.md +101 -0
- goroutine_py-2.0/goroutine/app.py +97 -0
- goroutine_py-2.0/goroutine_py.egg-info/PKG-INFO +116 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/pyproject.toml +1 -1
- goroutine_py-1.0.8/PKG-INFO +0 -119
- goroutine_py-1.0.8/README.md +0 -105
- goroutine_py-1.0.8/goroutine/app.py +0 -128
- goroutine_py-1.0.8/goroutine_py.egg-info/PKG-INFO +0 -119
- {goroutine_py-1.0.8 → goroutine_py-2.0}/LICENSE +0 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/goroutine/__init__.py +0 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/goroutine_py.egg-info/SOURCES.txt +0 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/goroutine_py.egg-info/dependency_links.txt +0 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/goroutine_py.egg-info/top_level.txt +0 -0
- {goroutine_py-1.0.8 → goroutine_py-2.0}/setup.cfg +0 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: goroutine-py
|
|
3
|
+
Version: 2.0
|
|
4
|
+
Summary: An Asyncio-based concurrency library for Python.
|
|
5
|
+
Author-email: xudesoft <xudesoft@126.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/purplegrapeZz/goroutine-py
|
|
7
|
+
Project-URL: Issues, https://github.com/purplegrapeZz/goroutine-py/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
English | 中文
|
|
17
|
+
|
|
18
|
+
goroutine-py
|
|
19
|
+
|
|
20
|
+
🚀 An Asyncio-based concurrency library for Python.
|
|
21
|
+
|
|
22
|
+
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Introduction
|
|
27
|
+
|
|
28
|
+
Withing goroutine.app.go you can run a coroutine or a func asynchronously.
|
|
29
|
+
|
|
30
|
+
Main function go :
|
|
31
|
+
|
|
32
|
+
go (obj: callable, *args, callback: callable = None, lock: bool = False)
|
|
33
|
+
|
|
34
|
+
obj: Takes both callable coroutinefunction and func as object.
|
|
35
|
+
|
|
36
|
+
*args: Arguments for your obj.
|
|
37
|
+
|
|
38
|
+
callback: Attaches a callable that will be called when the future finishes.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Getting Started
|
|
43
|
+
|
|
44
|
+
Support:
|
|
45
|
+
|
|
46
|
+
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
47
|
+
|
|
48
|
+
Installation
|
|
49
|
+
|
|
50
|
+
First you have to install goroutine-py like this:
|
|
51
|
+
|
|
52
|
+
pip install goroutine-py
|
|
53
|
+
|
|
54
|
+
Quick Tutorial
|
|
55
|
+
|
|
56
|
+
The primary entity of goroutine-py is goroutine.app.go.
|
|
57
|
+
|
|
58
|
+
You can simply start using goroutine-py like this:
|
|
59
|
+
|
|
60
|
+
First, define your tasks:
|
|
61
|
+
|
|
62
|
+
import asyncio
|
|
63
|
+
import time
|
|
64
|
+
from goroutine.app import go
|
|
65
|
+
|
|
66
|
+
# A normal func
|
|
67
|
+
def task_1(n=2):
|
|
68
|
+
time.sleep(n)
|
|
69
|
+
print('Task_1_done')
|
|
70
|
+
return 'Result_1'
|
|
71
|
+
|
|
72
|
+
# A coroutinefunction
|
|
73
|
+
async def task_2(n=1):
|
|
74
|
+
await asyncio.sleep(n)
|
|
75
|
+
print('Task_2_done')
|
|
76
|
+
return 'Result_2'
|
|
77
|
+
|
|
78
|
+
# Callback func
|
|
79
|
+
def callback(result):
|
|
80
|
+
'''
|
|
81
|
+
Parameter "result" is the return from task.
|
|
82
|
+
Use functools.partial() to give arguments if you need more args at the beginning.
|
|
83
|
+
'''
|
|
84
|
+
print('-* callback *-')
|
|
85
|
+
print(result)
|
|
86
|
+
|
|
87
|
+
After you defined all your tasks and callback, you can go like this:
|
|
88
|
+
|
|
89
|
+
go(task_1)
|
|
90
|
+
go(task_2)
|
|
91
|
+
|
|
92
|
+
# The "callback" parameter must be specified separately.
|
|
93
|
+
go(task_1, 5, callback = callback)
|
|
94
|
+
go(task_2, 3, callback = callback)
|
|
95
|
+
print('END')
|
|
96
|
+
|
|
97
|
+
# Forever runing to show results.
|
|
98
|
+
while 1:
|
|
99
|
+
time.sleep(5)
|
|
100
|
+
|
|
101
|
+
Output :
|
|
102
|
+
|
|
103
|
+
>>>
|
|
104
|
+
END
|
|
105
|
+
Task_2_done
|
|
106
|
+
Task_1_done
|
|
107
|
+
Task_2_done
|
|
108
|
+
-* callback *-
|
|
109
|
+
Result_2
|
|
110
|
+
Task_1_done
|
|
111
|
+
-* callback *-
|
|
112
|
+
Result_1
|
|
113
|
+
|
|
114
|
+
License
|
|
115
|
+
|
|
116
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
English | 中文
|
|
2
|
+
|
|
3
|
+
goroutine-py
|
|
4
|
+
|
|
5
|
+
🚀 An Asyncio-based concurrency library for Python.
|
|
6
|
+
|
|
7
|
+
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
Introduction
|
|
12
|
+
|
|
13
|
+
Withing goroutine.app.go you can run a coroutine or a func asynchronously.
|
|
14
|
+
|
|
15
|
+
Main function go :
|
|
16
|
+
|
|
17
|
+
go (obj: callable, *args, callback: callable = None, lock: bool = False)
|
|
18
|
+
|
|
19
|
+
obj: Takes both callable coroutinefunction and func as object.
|
|
20
|
+
|
|
21
|
+
*args: Arguments for your obj.
|
|
22
|
+
|
|
23
|
+
callback: Attaches a callable that will be called when the future finishes.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Getting Started
|
|
28
|
+
|
|
29
|
+
Support:
|
|
30
|
+
|
|
31
|
+
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
32
|
+
|
|
33
|
+
Installation
|
|
34
|
+
|
|
35
|
+
First you have to install goroutine-py like this:
|
|
36
|
+
|
|
37
|
+
pip install goroutine-py
|
|
38
|
+
|
|
39
|
+
Quick Tutorial
|
|
40
|
+
|
|
41
|
+
The primary entity of goroutine-py is goroutine.app.go.
|
|
42
|
+
|
|
43
|
+
You can simply start using goroutine-py like this:
|
|
44
|
+
|
|
45
|
+
First, define your tasks:
|
|
46
|
+
|
|
47
|
+
import asyncio
|
|
48
|
+
import time
|
|
49
|
+
from goroutine.app import go
|
|
50
|
+
|
|
51
|
+
# A normal func
|
|
52
|
+
def task_1(n=2):
|
|
53
|
+
time.sleep(n)
|
|
54
|
+
print('Task_1_done')
|
|
55
|
+
return 'Result_1'
|
|
56
|
+
|
|
57
|
+
# A coroutinefunction
|
|
58
|
+
async def task_2(n=1):
|
|
59
|
+
await asyncio.sleep(n)
|
|
60
|
+
print('Task_2_done')
|
|
61
|
+
return 'Result_2'
|
|
62
|
+
|
|
63
|
+
# Callback func
|
|
64
|
+
def callback(result):
|
|
65
|
+
'''
|
|
66
|
+
Parameter "result" is the return from task.
|
|
67
|
+
Use functools.partial() to give arguments if you need more args at the beginning.
|
|
68
|
+
'''
|
|
69
|
+
print('-* callback *-')
|
|
70
|
+
print(result)
|
|
71
|
+
|
|
72
|
+
After you defined all your tasks and callback, you can go like this:
|
|
73
|
+
|
|
74
|
+
go(task_1)
|
|
75
|
+
go(task_2)
|
|
76
|
+
|
|
77
|
+
# The "callback" parameter must be specified separately.
|
|
78
|
+
go(task_1, 5, callback = callback)
|
|
79
|
+
go(task_2, 3, callback = callback)
|
|
80
|
+
print('END')
|
|
81
|
+
|
|
82
|
+
# Forever runing to show results.
|
|
83
|
+
while 1:
|
|
84
|
+
time.sleep(5)
|
|
85
|
+
|
|
86
|
+
Output :
|
|
87
|
+
|
|
88
|
+
>>>
|
|
89
|
+
END
|
|
90
|
+
Task_2_done
|
|
91
|
+
Task_1_done
|
|
92
|
+
Task_2_done
|
|
93
|
+
-* callback *-
|
|
94
|
+
Result_2
|
|
95
|
+
Task_1_done
|
|
96
|
+
-* callback *-
|
|
97
|
+
Result_1
|
|
98
|
+
|
|
99
|
+
License
|
|
100
|
+
|
|
101
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Copyright 2022 ZIHAN MA. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
'''
|
|
4
|
+
🚀 An Asyncio-based concurrency library for Python.
|
|
5
|
+
Easy concurrency in Python. Just like goroutine.
|
|
6
|
+
'''
|
|
7
|
+
|
|
8
|
+
__author__ = 'ZIHAN MA (xudesoft@126.com)'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
12
|
+
import asyncio
|
|
13
|
+
from threading import Thread
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
Executor = ThreadPoolExecutor(max_workers=8)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _run() -> None:
|
|
21
|
+
'''
|
|
22
|
+
Start loop.
|
|
23
|
+
'''
|
|
24
|
+
_goroutine_loop.run_forever()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _iscor(obj: callable) -> bool:
|
|
28
|
+
'''
|
|
29
|
+
Check if obj a coroutinefunction.
|
|
30
|
+
Return:
|
|
31
|
+
True or Fale.
|
|
32
|
+
'''
|
|
33
|
+
return asyncio.iscoroutinefunction(obj)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
async def _wrap_cor(_cor, *args, callback = None) -> None:
|
|
37
|
+
'''
|
|
38
|
+
Handle the obj.
|
|
39
|
+
And will put this wrapper to the loop.
|
|
40
|
+
Return:
|
|
41
|
+
None.
|
|
42
|
+
'''
|
|
43
|
+
res = await _cor
|
|
44
|
+
|
|
45
|
+
# Handle the callback func from user.
|
|
46
|
+
if callable(callback):
|
|
47
|
+
if _iscor(callback):
|
|
48
|
+
await callback(res) if res else callback()
|
|
49
|
+
else:
|
|
50
|
+
await _wrap_func(callback, res) if res else _wrap_func(callback)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
async def _wrap_func(func: callable, *args) -> Any:
|
|
54
|
+
'''
|
|
55
|
+
For normal func, run as a thread.
|
|
56
|
+
Return:
|
|
57
|
+
Result of func.
|
|
58
|
+
'''
|
|
59
|
+
res = await asyncio.to_thread(func, *args)
|
|
60
|
+
return res
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def go(obj: callable, *args, callback: callable = None) -> None:
|
|
64
|
+
'''
|
|
65
|
+
Run a coroutine or a func asynchronously.
|
|
66
|
+
Easy concurrency in Python.
|
|
67
|
+
Args:
|
|
68
|
+
obj: Takes both callable coroutinefunction and func as object.
|
|
69
|
+
Coroutinefunction runs as coroutine.
|
|
70
|
+
Normal function runs as thread.
|
|
71
|
+
*args: Arguments for your obj.
|
|
72
|
+
You can also use functools.partial() for your func.
|
|
73
|
+
callback: Attaches a callable that will be called when the cor finishes.
|
|
74
|
+
Return:
|
|
75
|
+
None
|
|
76
|
+
'''
|
|
77
|
+
if callable(obj):
|
|
78
|
+
# Check if the given obeject callable.
|
|
79
|
+
if _iscor(obj):
|
|
80
|
+
# If a coroutinefunction run this.
|
|
81
|
+
future = asyncio.run_coroutine_threadsafe(_wrap_cor(obj(*args), callback=callback), _goroutine_loop)
|
|
82
|
+
else:
|
|
83
|
+
# If normal func, runs as a thread.
|
|
84
|
+
# future = asyncio.run_coroutine_threadsafe(_wrap_cor(_wrap_func(obj, *args), callback=callback), _goroutine_loop)
|
|
85
|
+
Executor.submit(obj, *args)
|
|
86
|
+
else:
|
|
87
|
+
raise TypeError(
|
|
88
|
+
'A callable func or coroutinefunction object is required')
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# Getting loop.
|
|
92
|
+
_goroutine_loop = asyncio.new_event_loop()
|
|
93
|
+
|
|
94
|
+
# Run the loop in a thread.
|
|
95
|
+
# T = Thread(target=_run, daemon=True)
|
|
96
|
+
# T.start()
|
|
97
|
+
Executor.submit(_run)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: goroutine-py
|
|
3
|
+
Version: 2.0
|
|
4
|
+
Summary: An Asyncio-based concurrency library for Python.
|
|
5
|
+
Author-email: xudesoft <xudesoft@126.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/purplegrapeZz/goroutine-py
|
|
7
|
+
Project-URL: Issues, https://github.com/purplegrapeZz/goroutine-py/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
English | 中文
|
|
17
|
+
|
|
18
|
+
goroutine-py
|
|
19
|
+
|
|
20
|
+
🚀 An Asyncio-based concurrency library for Python.
|
|
21
|
+
|
|
22
|
+
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Introduction
|
|
27
|
+
|
|
28
|
+
Withing goroutine.app.go you can run a coroutine or a func asynchronously.
|
|
29
|
+
|
|
30
|
+
Main function go :
|
|
31
|
+
|
|
32
|
+
go (obj: callable, *args, callback: callable = None, lock: bool = False)
|
|
33
|
+
|
|
34
|
+
obj: Takes both callable coroutinefunction and func as object.
|
|
35
|
+
|
|
36
|
+
*args: Arguments for your obj.
|
|
37
|
+
|
|
38
|
+
callback: Attaches a callable that will be called when the future finishes.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Getting Started
|
|
43
|
+
|
|
44
|
+
Support:
|
|
45
|
+
|
|
46
|
+
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
47
|
+
|
|
48
|
+
Installation
|
|
49
|
+
|
|
50
|
+
First you have to install goroutine-py like this:
|
|
51
|
+
|
|
52
|
+
pip install goroutine-py
|
|
53
|
+
|
|
54
|
+
Quick Tutorial
|
|
55
|
+
|
|
56
|
+
The primary entity of goroutine-py is goroutine.app.go.
|
|
57
|
+
|
|
58
|
+
You can simply start using goroutine-py like this:
|
|
59
|
+
|
|
60
|
+
First, define your tasks:
|
|
61
|
+
|
|
62
|
+
import asyncio
|
|
63
|
+
import time
|
|
64
|
+
from goroutine.app import go
|
|
65
|
+
|
|
66
|
+
# A normal func
|
|
67
|
+
def task_1(n=2):
|
|
68
|
+
time.sleep(n)
|
|
69
|
+
print('Task_1_done')
|
|
70
|
+
return 'Result_1'
|
|
71
|
+
|
|
72
|
+
# A coroutinefunction
|
|
73
|
+
async def task_2(n=1):
|
|
74
|
+
await asyncio.sleep(n)
|
|
75
|
+
print('Task_2_done')
|
|
76
|
+
return 'Result_2'
|
|
77
|
+
|
|
78
|
+
# Callback func
|
|
79
|
+
def callback(result):
|
|
80
|
+
'''
|
|
81
|
+
Parameter "result" is the return from task.
|
|
82
|
+
Use functools.partial() to give arguments if you need more args at the beginning.
|
|
83
|
+
'''
|
|
84
|
+
print('-* callback *-')
|
|
85
|
+
print(result)
|
|
86
|
+
|
|
87
|
+
After you defined all your tasks and callback, you can go like this:
|
|
88
|
+
|
|
89
|
+
go(task_1)
|
|
90
|
+
go(task_2)
|
|
91
|
+
|
|
92
|
+
# The "callback" parameter must be specified separately.
|
|
93
|
+
go(task_1, 5, callback = callback)
|
|
94
|
+
go(task_2, 3, callback = callback)
|
|
95
|
+
print('END')
|
|
96
|
+
|
|
97
|
+
# Forever runing to show results.
|
|
98
|
+
while 1:
|
|
99
|
+
time.sleep(5)
|
|
100
|
+
|
|
101
|
+
Output :
|
|
102
|
+
|
|
103
|
+
>>>
|
|
104
|
+
END
|
|
105
|
+
Task_2_done
|
|
106
|
+
Task_1_done
|
|
107
|
+
Task_2_done
|
|
108
|
+
-* callback *-
|
|
109
|
+
Result_2
|
|
110
|
+
Task_1_done
|
|
111
|
+
-* callback *-
|
|
112
|
+
Result_1
|
|
113
|
+
|
|
114
|
+
License
|
|
115
|
+
|
|
116
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
goroutine_py-1.0.8/PKG-INFO
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: goroutine-py
|
|
3
|
-
Version: 1.0.8
|
|
4
|
-
Summary: An Asyncio-based concurrency library for Python.
|
|
5
|
-
Author-email: xudesoft <xudesoft@126.com>
|
|
6
|
-
Project-URL: Homepage, https://github.com/purplegrapeZz/goroutine-py
|
|
7
|
-
Project-URL: Issues, https://github.com/purplegrapeZz/goroutine-py/issues
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.7
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
|
|
15
|
-
English | [中文](https://github.com/purplegrapeZz/goroutine-py/blob/master/README-CN.md)
|
|
16
|
-
|
|
17
|
-
### goroutine-py
|
|
18
|
-
|
|
19
|
-
🚀 An Asyncio-based concurrency library for Python.
|
|
20
|
-
|
|
21
|
-
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
# Introduction
|
|
26
|
-
|
|
27
|
-
Withing ``goroutine.app.go`` you can run a coroutine or a func asynchronously.
|
|
28
|
-
|
|
29
|
-
Main function ___go___ :
|
|
30
|
-
|
|
31
|
-
##### go _(obj: callable, *args, callback: callable = None, lock: bool = False)_
|
|
32
|
-
|
|
33
|
-
___obj:___ Takes both callable coroutinefunction and func as object.
|
|
34
|
-
|
|
35
|
-
___*args:___ Arguments for your obj.
|
|
36
|
-
|
|
37
|
-
___callback:___ Attaches a callable that will be called when the future finishes.
|
|
38
|
-
|
|
39
|
-
___lock:___ Thread safe if True. It can slow your program.
|
|
40
|
-
|
|
41
|
-
This argument only work for "func" not "coroutinefunction".
|
|
42
|
-
|
|
43
|
-
# Getting Started
|
|
44
|
-
## Support:
|
|
45
|
-
|
|
46
|
-
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
47
|
-
|
|
48
|
-
## Installation
|
|
49
|
-
|
|
50
|
-
First you have to install goroutine-py like this:
|
|
51
|
-
|
|
52
|
-
```
|
|
53
|
-
pip install goroutine-py
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Quick Tutorial
|
|
57
|
-
|
|
58
|
-
The primary entity of goroutine-py is ``goroutine.app.go``.
|
|
59
|
-
You can simply start using goroutine-py like this:
|
|
60
|
-
|
|
61
|
-
First, define your tasks:
|
|
62
|
-
|
|
63
|
-
```
|
|
64
|
-
import asyncio
|
|
65
|
-
import functools
|
|
66
|
-
import time
|
|
67
|
-
from goroutine.app import go
|
|
68
|
-
|
|
69
|
-
# A normal func
|
|
70
|
-
def task_1(n=2):
|
|
71
|
-
time.sleep(n)
|
|
72
|
-
print('Task_1_done')
|
|
73
|
-
return 'Result_1'
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
```
|
|
77
|
-
# A coroutinefunction
|
|
78
|
-
async def task_2(n=1):
|
|
79
|
-
await asyncio.sleep(n)
|
|
80
|
-
print('Task_2_done')
|
|
81
|
-
return 'Result_2'
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
# Callback func
|
|
86
|
-
def callback(future, arg=None):
|
|
87
|
-
'''
|
|
88
|
-
At least ONE Parameter "future" is required.
|
|
89
|
-
This future is a concurrent.futures.Future.
|
|
90
|
-
Use functools.partial() to give arguments for your callback func.
|
|
91
|
-
'''
|
|
92
|
-
print(future.result(),arg)
|
|
93
|
-
```
|
|
94
|
-
After you defined all your tasks and callback, you can go like this:
|
|
95
|
-
|
|
96
|
-
```
|
|
97
|
-
go(task_1)
|
|
98
|
-
go(task_2)
|
|
99
|
-
go(task_1, 4, callback=callback)
|
|
100
|
-
go(task_2, 2, callback=functools.partial(callback,arg='a'))
|
|
101
|
-
print('END')
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
Output :
|
|
105
|
-
|
|
106
|
-
```
|
|
107
|
-
>>>
|
|
108
|
-
END
|
|
109
|
-
Task_2_done
|
|
110
|
-
Task_1_done
|
|
111
|
-
Task_2_done
|
|
112
|
-
Result_2 a
|
|
113
|
-
Task_1_done
|
|
114
|
-
Result_1 None
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
# License
|
|
118
|
-
|
|
119
|
-
This project is licensed under the MIT License - see the `LICENSE` file for details.
|
goroutine_py-1.0.8/README.md
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
English | [中文](https://github.com/purplegrapeZz/goroutine-py/blob/master/README-CN.md)
|
|
2
|
-
|
|
3
|
-
### goroutine-py
|
|
4
|
-
|
|
5
|
-
🚀 An Asyncio-based concurrency library for Python.
|
|
6
|
-
|
|
7
|
-
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# Introduction
|
|
12
|
-
|
|
13
|
-
Withing ``goroutine.app.go`` you can run a coroutine or a func asynchronously.
|
|
14
|
-
|
|
15
|
-
Main function ___go___ :
|
|
16
|
-
|
|
17
|
-
##### go _(obj: callable, *args, callback: callable = None, lock: bool = False)_
|
|
18
|
-
|
|
19
|
-
___obj:___ Takes both callable coroutinefunction and func as object.
|
|
20
|
-
|
|
21
|
-
___*args:___ Arguments for your obj.
|
|
22
|
-
|
|
23
|
-
___callback:___ Attaches a callable that will be called when the future finishes.
|
|
24
|
-
|
|
25
|
-
___lock:___ Thread safe if True. It can slow your program.
|
|
26
|
-
|
|
27
|
-
This argument only work for "func" not "coroutinefunction".
|
|
28
|
-
|
|
29
|
-
# Getting Started
|
|
30
|
-
## Support:
|
|
31
|
-
|
|
32
|
-
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
33
|
-
|
|
34
|
-
## Installation
|
|
35
|
-
|
|
36
|
-
First you have to install goroutine-py like this:
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
pip install goroutine-py
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Quick Tutorial
|
|
43
|
-
|
|
44
|
-
The primary entity of goroutine-py is ``goroutine.app.go``.
|
|
45
|
-
You can simply start using goroutine-py like this:
|
|
46
|
-
|
|
47
|
-
First, define your tasks:
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
import asyncio
|
|
51
|
-
import functools
|
|
52
|
-
import time
|
|
53
|
-
from goroutine.app import go
|
|
54
|
-
|
|
55
|
-
# A normal func
|
|
56
|
-
def task_1(n=2):
|
|
57
|
-
time.sleep(n)
|
|
58
|
-
print('Task_1_done')
|
|
59
|
-
return 'Result_1'
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
```
|
|
63
|
-
# A coroutinefunction
|
|
64
|
-
async def task_2(n=1):
|
|
65
|
-
await asyncio.sleep(n)
|
|
66
|
-
print('Task_2_done')
|
|
67
|
-
return 'Result_2'
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
```
|
|
71
|
-
# Callback func
|
|
72
|
-
def callback(future, arg=None):
|
|
73
|
-
'''
|
|
74
|
-
At least ONE Parameter "future" is required.
|
|
75
|
-
This future is a concurrent.futures.Future.
|
|
76
|
-
Use functools.partial() to give arguments for your callback func.
|
|
77
|
-
'''
|
|
78
|
-
print(future.result(),arg)
|
|
79
|
-
```
|
|
80
|
-
After you defined all your tasks and callback, you can go like this:
|
|
81
|
-
|
|
82
|
-
```
|
|
83
|
-
go(task_1)
|
|
84
|
-
go(task_2)
|
|
85
|
-
go(task_1, 4, callback=callback)
|
|
86
|
-
go(task_2, 2, callback=functools.partial(callback,arg='a'))
|
|
87
|
-
print('END')
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
Output :
|
|
91
|
-
|
|
92
|
-
```
|
|
93
|
-
>>>
|
|
94
|
-
END
|
|
95
|
-
Task_2_done
|
|
96
|
-
Task_1_done
|
|
97
|
-
Task_2_done
|
|
98
|
-
Result_2 a
|
|
99
|
-
Task_1_done
|
|
100
|
-
Result_1 None
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
# License
|
|
104
|
-
|
|
105
|
-
This project is licensed under the MIT License - see the `LICENSE` file for details.
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
# Copyright 2022 ZIHAN MA. All Rights Reserved.
|
|
2
|
-
|
|
3
|
-
'''
|
|
4
|
-
🚀 An Asyncio-based concurrency library for Python.
|
|
5
|
-
Easy concurrency in Python. Just like goroutine.
|
|
6
|
-
'''
|
|
7
|
-
|
|
8
|
-
__author__ = 'ZIHAN MA (xudesoft@126.com)'
|
|
9
|
-
|
|
10
|
-
import asyncio
|
|
11
|
-
from threading import Thread
|
|
12
|
-
import functools
|
|
13
|
-
from asyncio.futures import Future
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def _run() -> None:
|
|
17
|
-
'''
|
|
18
|
-
Start loop.
|
|
19
|
-
'''
|
|
20
|
-
_goroutine_loop.run_forever()
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def _iscorfunc(obj: callable) -> bool:
|
|
24
|
-
'''
|
|
25
|
-
Check if obj a coroutinefunction.
|
|
26
|
-
Return:
|
|
27
|
-
True or Fale.
|
|
28
|
-
'''
|
|
29
|
-
return asyncio.iscoroutinefunction(obj)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
async def _wrap_as_cor(obj: callable, *args) -> Future:
|
|
33
|
-
'''
|
|
34
|
-
Wrap a func as a coroutine.
|
|
35
|
-
Return:
|
|
36
|
-
A coroutine.
|
|
37
|
-
'''
|
|
38
|
-
res = await asyncio.to_thread(obj, *args)
|
|
39
|
-
return res
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
async def _wrap_as_cor_withlock(obj: callable, *args) -> Future:
|
|
43
|
-
'''
|
|
44
|
-
Wrap a func as a coroutine with lock. Means thread safe.
|
|
45
|
-
Return:
|
|
46
|
-
A coroutine.
|
|
47
|
-
'''
|
|
48
|
-
async with _goroutine_loop_lock:
|
|
49
|
-
res = await asyncio.to_thread(obj, *args)
|
|
50
|
-
return res
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def _wrap_as_func(obj: callable, future: Future) -> None:
|
|
54
|
-
'''
|
|
55
|
-
For callback func is given as a coroutine.
|
|
56
|
-
'''
|
|
57
|
-
asyncio.run_coroutine_threadsafe(obj(future), _goroutine_loop)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def _finish(future: Future, callback: callable) -> None:
|
|
61
|
-
'''
|
|
62
|
-
Do the callback.
|
|
63
|
-
'''
|
|
64
|
-
if callable(callback):
|
|
65
|
-
if not _iscorfunc(callback):
|
|
66
|
-
# Check if callback a func.
|
|
67
|
-
future.add_done_callback(callback)
|
|
68
|
-
else:
|
|
69
|
-
future.add_done_callback(
|
|
70
|
-
functools.partial(
|
|
71
|
-
_wrap_as_func, callback))
|
|
72
|
-
else:
|
|
73
|
-
raise TypeError(
|
|
74
|
-
'A callable func or coroutinefunction object is required for callback.')
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def go(obj: callable, *args, callback: callable = None, lock: bool = False) -> None:
|
|
78
|
-
'''
|
|
79
|
-
Run a coroutine or a func asynchronously.
|
|
80
|
-
Easy concurrency in Python.
|
|
81
|
-
Args:
|
|
82
|
-
obj: Takes both callable coroutinefunction and func as object.
|
|
83
|
-
Coroutinefunction runs as coroutine.
|
|
84
|
-
Normal function runs as thread.
|
|
85
|
-
*args: Arguments for your obj.
|
|
86
|
-
You can also use functools.partial() for your func.
|
|
87
|
-
callback: Attaches a callable that will be called when the future finishes.
|
|
88
|
-
Use functools.partial() to your callback func.
|
|
89
|
-
lock: Thread safe if True. It can slow your program.
|
|
90
|
-
This argument only work for "func" not "coroutinefunction".
|
|
91
|
-
Return:
|
|
92
|
-
None
|
|
93
|
-
'''
|
|
94
|
-
if callable(obj):
|
|
95
|
-
'''
|
|
96
|
-
Check if the given obeject callable.
|
|
97
|
-
'''
|
|
98
|
-
if _iscorfunc(obj):
|
|
99
|
-
# If a coroutinefunction run this.
|
|
100
|
-
future = asyncio.run_coroutine_threadsafe(obj(*args), _goroutine_loop)
|
|
101
|
-
if callback:
|
|
102
|
-
# Add callback func.
|
|
103
|
-
_finish(future, callback)
|
|
104
|
-
else:
|
|
105
|
-
# Normal func runs as a thread.
|
|
106
|
-
if lock:
|
|
107
|
-
# Thread safe. Only for a normal func.
|
|
108
|
-
cor = _wrap_as_cor_withlock(obj, *args)
|
|
109
|
-
else:
|
|
110
|
-
# Thread without lock.
|
|
111
|
-
cor = _wrap_as_cor(obj, *args)
|
|
112
|
-
future = asyncio.run_coroutine_threadsafe(cor, _goroutine_loop)
|
|
113
|
-
if callback:
|
|
114
|
-
_finish(future, callback)
|
|
115
|
-
else:
|
|
116
|
-
raise TypeError(
|
|
117
|
-
'A callable func or coroutinefunction object is required')
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
# An asyncio-lock.
|
|
121
|
-
_goroutine_loop_lock = asyncio.Lock()
|
|
122
|
-
|
|
123
|
-
# Getting loop.
|
|
124
|
-
_goroutine_loop = asyncio.new_event_loop()
|
|
125
|
-
|
|
126
|
-
# Run the loop in a thread.
|
|
127
|
-
T = Thread(target=_run, daemon=True)
|
|
128
|
-
T.start()
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: goroutine-py
|
|
3
|
-
Version: 1.0.8
|
|
4
|
-
Summary: An Asyncio-based concurrency library for Python.
|
|
5
|
-
Author-email: xudesoft <xudesoft@126.com>
|
|
6
|
-
Project-URL: Homepage, https://github.com/purplegrapeZz/goroutine-py
|
|
7
|
-
Project-URL: Issues, https://github.com/purplegrapeZz/goroutine-py/issues
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
-
Classifier: Operating System :: OS Independent
|
|
11
|
-
Requires-Python: >=3.7
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
License-File: LICENSE
|
|
14
|
-
|
|
15
|
-
English | [中文](https://github.com/purplegrapeZz/goroutine-py/blob/master/README-CN.md)
|
|
16
|
-
|
|
17
|
-
### goroutine-py
|
|
18
|
-
|
|
19
|
-
🚀 An Asyncio-based concurrency library for Python.
|
|
20
|
-
|
|
21
|
-
Easy concurrency just like goroutine without worry about thread and coroutine in Python.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
# Introduction
|
|
26
|
-
|
|
27
|
-
Withing ``goroutine.app.go`` you can run a coroutine or a func asynchronously.
|
|
28
|
-
|
|
29
|
-
Main function ___go___ :
|
|
30
|
-
|
|
31
|
-
##### go _(obj: callable, *args, callback: callable = None, lock: bool = False)_
|
|
32
|
-
|
|
33
|
-
___obj:___ Takes both callable coroutinefunction and func as object.
|
|
34
|
-
|
|
35
|
-
___*args:___ Arguments for your obj.
|
|
36
|
-
|
|
37
|
-
___callback:___ Attaches a callable that will be called when the future finishes.
|
|
38
|
-
|
|
39
|
-
___lock:___ Thread safe if True. It can slow your program.
|
|
40
|
-
|
|
41
|
-
This argument only work for "func" not "coroutinefunction".
|
|
42
|
-
|
|
43
|
-
# Getting Started
|
|
44
|
-
## Support:
|
|
45
|
-
|
|
46
|
-
Python 3.7 / 3.8 / 3.9 / 3.10 / 3.11 / 3.12
|
|
47
|
-
|
|
48
|
-
## Installation
|
|
49
|
-
|
|
50
|
-
First you have to install goroutine-py like this:
|
|
51
|
-
|
|
52
|
-
```
|
|
53
|
-
pip install goroutine-py
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## Quick Tutorial
|
|
57
|
-
|
|
58
|
-
The primary entity of goroutine-py is ``goroutine.app.go``.
|
|
59
|
-
You can simply start using goroutine-py like this:
|
|
60
|
-
|
|
61
|
-
First, define your tasks:
|
|
62
|
-
|
|
63
|
-
```
|
|
64
|
-
import asyncio
|
|
65
|
-
import functools
|
|
66
|
-
import time
|
|
67
|
-
from goroutine.app import go
|
|
68
|
-
|
|
69
|
-
# A normal func
|
|
70
|
-
def task_1(n=2):
|
|
71
|
-
time.sleep(n)
|
|
72
|
-
print('Task_1_done')
|
|
73
|
-
return 'Result_1'
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
```
|
|
77
|
-
# A coroutinefunction
|
|
78
|
-
async def task_2(n=1):
|
|
79
|
-
await asyncio.sleep(n)
|
|
80
|
-
print('Task_2_done')
|
|
81
|
-
return 'Result_2'
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
# Callback func
|
|
86
|
-
def callback(future, arg=None):
|
|
87
|
-
'''
|
|
88
|
-
At least ONE Parameter "future" is required.
|
|
89
|
-
This future is a concurrent.futures.Future.
|
|
90
|
-
Use functools.partial() to give arguments for your callback func.
|
|
91
|
-
'''
|
|
92
|
-
print(future.result(),arg)
|
|
93
|
-
```
|
|
94
|
-
After you defined all your tasks and callback, you can go like this:
|
|
95
|
-
|
|
96
|
-
```
|
|
97
|
-
go(task_1)
|
|
98
|
-
go(task_2)
|
|
99
|
-
go(task_1, 4, callback=callback)
|
|
100
|
-
go(task_2, 2, callback=functools.partial(callback,arg='a'))
|
|
101
|
-
print('END')
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
Output :
|
|
105
|
-
|
|
106
|
-
```
|
|
107
|
-
>>>
|
|
108
|
-
END
|
|
109
|
-
Task_2_done
|
|
110
|
-
Task_1_done
|
|
111
|
-
Task_2_done
|
|
112
|
-
Result_2 a
|
|
113
|
-
Task_1_done
|
|
114
|
-
Result_1 None
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
# License
|
|
118
|
-
|
|
119
|
-
This project is licensed under the MIT License - see the `LICENSE` file for details.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|