Skip to content

Commit c1b212a

Browse files
authored
Add files via upload
1 parent a1998b3 commit c1b212a

5 files changed

Lines changed: 136 additions & 2 deletions

File tree

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,106 @@ imthread.elapsed(output=True)
180180

181181
we kept a time gap of 1 sec inside the function still it repeated the task 4 times in same time. since it can access the global variables we can assign certain tasks that don't need different inputs every time.
182182

183+
#### Decorators support
184+
185+
Apart from calling the ``start()`` attribute we can also use decorators to explicitly make our functions for concurrent execution.
186+
187+
**Example 1**
188+
189+
````python
190+
import imthread
191+
import time
192+
193+
@imthread.run(repeat=5, max_threads=5)
194+
def my_func(i):
195+
time.sleep(1)
196+
return i*2
197+
198+
print(imthread.result['my_func'])
199+
````
200+
201+
**Output**
202+
203+
````python
204+
[0, 2, 4, 6, 8]
205+
````
206+
207+
This will execute the function as soon as you run your python code, in this case we are trying to perform the same task five times in a row concurrently. The final output of all the function can be accessed by ``imthread.result['function_name']``. Notice if you set your function on repeat it will always receive a parameter which is it's thread number.
208+
209+
----
210+
211+
**Example 2**
212+
213+
````python
214+
import imthread
215+
import time
216+
217+
@imthread.run(data=[1,2,3,4,5,6], max_threads=5)
218+
def my_func(i):
219+
time.sleep(1)
220+
return i*2
221+
222+
print(imthread.result['my_func'])
223+
````
224+
225+
**Output**
226+
227+
````python
228+
[2, 4, 6, 8, 10, 12]
229+
````
230+
231+
In this case we are directly passing our arguments in a list via decorator and receiving the result same way as we did in previous example.
232+
233+
----
234+
235+
**Exampe 3**
236+
237+
````python
238+
import imthread
239+
import time
240+
241+
@imthread.set(max_threads=10)
242+
def my_func(i):
243+
time.sleep(1)
244+
return i*2
245+
246+
result = my_func(repeat=7)
247+
print(result)
248+
````
249+
250+
**Output**
251+
252+
````python
253+
[0, 2, 4, 6, 8, 10, 12]
254+
````
255+
256+
This is an another cool way to first convert your function in concurrent function and then passing the argument as how many time you want to execute that function all in parallel.
257+
258+
----
259+
260+
**Example 4**
261+
262+
````python
263+
import imthread
264+
import time
265+
266+
@imthread.set()
267+
def my_func(i):
268+
time.sleep(1)
269+
return i*2
270+
271+
result = my_func([5,6,7])
272+
print(result)
273+
````
274+
275+
**Output**
276+
277+
````python
278+
[10, 12, 14]
279+
````
280+
281+
Again we can also specify what arguments we want to pass to the function to process it concurrently. if in the ``@imthread.set()`` decorator you won't pass any ``max_threads`` argument ``max_threads=10`` will be set.
282+
183283
#### Handling errors and killing all threads
184284

185285
So, by default if any error occurs the threads will keep on running, in case if you want to ignore some errors but if you want to kill all the thread at once you can use ``imthread.stop()`` while handling errors.

dist/imthread-1.1.tar.gz

2.92 KB
Binary file not shown.

imthread/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from imthread.imthread import multi_threading, console_log
22
import time
33

4+
__version__='1.1'
5+
__author__='Nitin Rai'
6+
47
st = 0
8+
#default start function for spawning threads
59
def start(processing_func, data=None, repeat=None, max_threads=10):
610
assert type(max_threads) == int, 'max_threads value should be an integer'
711
assert max_threads>0, 'max_threads value cannot be less than 1'
@@ -21,9 +25,38 @@ def start(processing_func, data=None, repeat=None, max_threads=10):
2125
else:
2226
print(f'data: {data}, repeat: {repeat}')
2327

28+
29+
result = {}
30+
#decorator support function for quick launch
31+
def run(*args, **kwargs):
32+
def inter(func, kwargs):
33+
global result
34+
data = kwargs['data'] if 'data' in kwargs else None
35+
repeat = kwargs['repeat'] if 'repeat'in kwargs else None
36+
assert data!=None or repeat!=None, "one or more arguments missing!"
37+
max_threads = kwargs['max_threads'] if 'max_threads' in kwargs else 10
38+
result[func.__name__] = start(func, data=data, repeat=repeat, max_threads=max_threads)
39+
40+
return lambda func:inter(func, kwargs)
41+
42+
#decorator support function for setting up input function
43+
def set(*args, **kwargs):
44+
def inter1(func, kwargs):
45+
def inter2(*args, **extra):
46+
max_threads = kwargs['max_threads'] if 'max_threads' in kwargs else 10
47+
try:
48+
args = extra['repeat'] if len(args)==0 else args[0]
49+
except Exception as e:
50+
assert len(args)>0, func.__name__+' requires atleast one argument'
51+
res = start(func, data=args, max_threads=max_threads)
52+
return res
53+
return inter2
54+
return lambda func:inter1(func, kwargs)
55+
2456
def stop():
2557
raise Exception('stop')
2658

59+
2760
def elapsed(output=False):
2861
tt = round((time.time()-st), 2)
2962
if output:

imthread/imthread.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def process_frames(data):
3333

3434
try:
3535
processed_data = self.process(data[1]) #actually processing the data
36+
3637
except Exception as e:
3738
processed_data = None
3839
if str(e) == 'stop': #if manually stop exception raised

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
setup(
33
name = 'imthread', # How you named your package folder (MyLib)
44
packages = ['imthread'], # Chose the same as "name"
5-
version = '1.0', # Start with a small number and increase it with every change you make
5+
version = '1.1', # Start with a small number and increase it with every change you make
66
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
77
description = 'This short little python module can help you with running your iterable functions on multithreading without any hassle of creating threads by yourself.', # Give a short description about your library
88
author = 'Nitin Rai', # Type in your name
99
author_email = 'mneonizer@gmail.com', # Type in your E-Mail
1010
url = 'https://github.com/imneonizer/imthread', # Provide either the link to your github or to your website
11-
download_url = 'https://github.com/imneonizer/imthread/archive/v1.0.tar.gz', # I explain this later on
11+
download_url = 'https://github.com/imneonizer/imthread/archive/v1.1.tar.gz', # I explain this later on
1212
keywords = ['Multi Threading', 'Synchronous Threading', 'Asyncio'], # Keywords that define your package best
1313
classifiers=[
1414
'Development Status :: 4 - Beta', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package

0 commit comments

Comments
 (0)