asynchronous 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +51 -0
- data/VERSION +1 -1
- data/lib/asynchronous/concurrency.rb +9 -2
- data/lib/asynchronous/parallelism.rb +1 -13
- metadata +2 -2
data/README.md
CHANGED
@@ -2,7 +2,58 @@ asynchronous
|
|
2
2
|
============
|
3
3
|
|
4
4
|
Asynchronous Patterns for Ruby Based on Pure MRI CRuby code
|
5
|
+
The goal is to use the original MRI C libs for achive
|
6
|
+
real async patterns in ruby
|
5
7
|
|
8
|
+
Well it is achived...
|
9
|
+
|
10
|
+
|
11
|
+
# OS managed thread
|
12
|
+
|
13
|
+
copy on write memory share,
|
14
|
+
so you cant change anything in the mother process
|
15
|
+
only with the return value later.
|
16
|
+
This method is stronger the more CPU core the os have
|
17
|
+
|
18
|
+
Ideal for Big jobs that require lot of memory allocation or
|
19
|
+
heavy CPU load to get a value
|
20
|
+
Like parsing (in some case)
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
|
24
|
+
calculation = async :parallelism do
|
25
|
+
|
26
|
+
sleep 4
|
27
|
+
# remember, everything you
|
28
|
+
# write here only mather in this block
|
29
|
+
# will not affect the real process only
|
30
|
+
# the last return value of the block
|
31
|
+
4 * 5
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
# to call the value (syncronize):
|
36
|
+
calculation.value
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
# VM managed thread
|
41
|
+
|
42
|
+
you can use simple :c also instead of :concurrency as sym,
|
43
|
+
remember :concurrency is all about GIL case, so
|
44
|
+
you can modify the objects in memory
|
45
|
+
This is ideal for little operations in simultaneously or
|
46
|
+
when you need to update objects in the memory and not the
|
47
|
+
return value what is mather.
|
48
|
+
Remember well that GarbageCollector will affect the speed.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
|
52
|
+
calculation= async { sleep 3; 4 * 3 }
|
53
|
+
# to call the value (syncronize):
|
54
|
+
calculation.value
|
55
|
+
|
56
|
+
```
|
6
57
|
# Examples
|
7
58
|
|
8
59
|
the "simple async processing" will let you use os threads (1.9.n+)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -10,13 +10,20 @@ module Asynchronous
|
|
10
10
|
def initialize(callable)
|
11
11
|
begin
|
12
12
|
@value= nil
|
13
|
+
@try_count= 0
|
13
14
|
@rescue_state= nil
|
14
15
|
@thread ||= ::Thread.new { callable.call }
|
15
16
|
@rescue_state= nil
|
16
17
|
rescue ThreadError
|
17
18
|
@rescue_state ||= true
|
18
|
-
|
19
|
-
|
19
|
+
@try_count += 1
|
20
|
+
if 3 <= @try_count
|
21
|
+
@value= callable.call
|
22
|
+
@rescue_state= nil
|
23
|
+
else
|
24
|
+
sleep 5
|
25
|
+
retry
|
26
|
+
end
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
@@ -11,8 +11,6 @@ module Asynchronous
|
|
11
11
|
|
12
12
|
# Basic
|
13
13
|
begin
|
14
|
-
|
15
|
-
#require 'yaml'
|
16
14
|
@@pids=[]
|
17
15
|
def initialize callable
|
18
16
|
|
@@ -33,7 +31,6 @@ module Asynchronous
|
|
33
31
|
@@pids.push(@pid)
|
34
32
|
|
35
33
|
end
|
36
|
-
|
37
34
|
end
|
38
35
|
|
39
36
|
# return value
|
@@ -56,22 +53,13 @@ module Asynchronous
|
|
56
53
|
|
57
54
|
end
|
58
55
|
|
59
|
-
|
60
56
|
def value=(obj)
|
61
57
|
@value= obj
|
62
58
|
end
|
63
59
|
|
64
|
-
#def method_missing(method,*args)
|
65
|
-
# value.__send__(method,*args)
|
66
|
-
#end
|
67
|
-
#
|
68
|
-
#def respond_to_missing?(method, include_private = false)
|
69
|
-
# value.respond_to?(method, include_private)
|
70
|
-
#end
|
71
|
-
|
72
60
|
end
|
73
61
|
|
74
|
-
# kill
|
62
|
+
# kill kidos at Kernel Exit
|
75
63
|
begin
|
76
64
|
::Kernel.at_exit {
|
77
65
|
@@pids.each { |pid|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asynchronous
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'Async dsl for easy concurrency patterns in both VM managed and OS
|
15
15
|
managed way (Concurrency and Parallelism) '
|