pwork 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pwork/async.rb +34 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a35ff205063037d1cfee0704cd9e63968fb202b4addf04ac9c9e0b77224f8be
4
- data.tar.gz: 502668fc851d8bf32b62bdff294c19f1952ec0958d2a3661c5e25a538a65c849
3
+ metadata.gz: 287685ce40b96d3cbb7526a722f5398b5124f7611288bfd641f820fe599b73e8
4
+ data.tar.gz: 4f06fcec0e7472125c9473409b397f01dceaba18b555b5a8d1c29bb2fa0a6dbc
5
5
  SHA512:
6
- metadata.gz: 879faaaac8101be4579b55fe4bf9f4e7b63a5649826f33dbe7c03d143e0eb83f28c766c4a901d58178095684cc753c4e8f9582f2c46a0bf73bebd9c6334fafdc
7
- data.tar.gz: dd6b2e9bcd93f4e5296ac246159a0e1abc2492fce6f2d542acaefa711c03975e604819e7e46c87493c32b86b4b3e584d55218805dd489e01b86d515ce372ec53
6
+ metadata.gz: cd1dd3ca81c5895f3d7c4e8e5d308fe5616740dd127f921db00a6dcdb50e83d9186ef54d632d28228d364b4f5b07484c3935dc7d55ab4943e7fc81137383d0e7
7
+ data.tar.gz: 0d45e837cb699245c142367a00262b3002adc4484e8161309fe73e280b66a6b8842536ab08aeaa80cc320abad7a35b9eb3047c4a1268140d8bb050dc5fa8ee5e
@@ -5,6 +5,28 @@ require_relative 'async/manager'
5
5
  module PWork
6
6
  module Async
7
7
  def async(options = {}, &block)
8
+ if PWork::Async.mode == 'fork'
9
+ PWork::Async.async_forked(options, &block)
10
+ else
11
+ PWork::Async.async_threaded(options, &block)
12
+ end
13
+ end
14
+
15
+ def self.async_forked(options = {}, &block)
16
+ if block_given?
17
+ pid = fork do
18
+ block.call
19
+ end
20
+ PWork::Async.tasks << pid unless options[:wait] == false
21
+ else
22
+ PWork::Async.tasks.each do |pid|
23
+ Process.wait(pid)
24
+ end
25
+ reset
26
+ end
27
+ end
28
+
29
+ def self.async_threaded(options = {}, &block)
8
30
  if block_given?
9
31
  options[:caller] = self
10
32
  PWork::Async.add_task(options, &block)
@@ -70,5 +92,17 @@ module PWork
70
92
  def self.async_wait_sleep_iteration
71
93
  Float(ENV.fetch('PWORK_ASYNC_WAIT_SLEEP_ITERATION', '0.02'))
72
94
  end
95
+
96
+ def self.mode
97
+ if ENV['PWORK_ASYNC_MODE'].to_s.downcase == 'fork'
98
+ 'fork'
99
+ else
100
+ 'thread'
101
+ end
102
+ end
103
+
104
+ def self.reset
105
+ Thread.current[:pwork_async_tasks] = []
106
+ end
73
107
  end
74
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-26 00:00:00.000000000 Z
11
+ date: 2018-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler