You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
182
182
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
+
defmy_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
+
defmy_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
+
defmy_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
+
defmy_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
+
183
283
#### Handling errors and killing all threads
184
284
185
285
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.
Copy file name to clipboardExpand all lines: setup.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
setup(
3
3
name='imthread', # How you named your package folder (MyLib)
4
4
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
6
6
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
7
7
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
8
8
author='Nitin Rai', # Type in your name
9
9
author_email='mneonizer@gmail.com', # Type in your E-Mail
10
10
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
12
12
keywords= ['Multi Threading', 'Synchronous Threading', 'Asyncio'], # Keywords that define your package best
13
13
classifiers=[
14
14
'Development Status :: 4 - Beta', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package
0 commit comments