1#!/usr/bin/env python3 2# 3# Copyright (c) 2016-2020 Intel Corporation 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Based on the software developed by: 18# Copyright (c) 2008,2016 david decotigny (Pool of threads) 19# Copyright (c) 2006-2008, R Oudkerk (multiprocessing.Pool) 20# All rights reserved. 21# 22# Redistribution and use in source and binary forms, with or without 23# modification, are permitted provided that the following conditions 24# are met: 25# 26# 1. Redistributions of source code must retain the above copyright 27# notice, this list of conditions and the following disclaimer. 28# 2. Redistributions in binary form must reproduce the above copyright 29# notice, this list of conditions and the following disclaimer in the 30# documentation and/or other materials provided with the distribution. 31# 3. Neither the name of author nor the names of any contributors may be 32# used to endorse or promote products derived from this software 33# without specific prior written permission. 34# 35# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 36# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 39# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 41# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 43# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 45# SUCH DAMAGE. 46# 47 48import time 49import threading 50 51from .api import * 52from .pool import * 53 54 55def test(arg=None): 56 if arg == "-v": 57 def say(*x): 58 print(*x) 59 else: 60 def say(*x): 61 pass 62 say("Start Pool testing") 63 print("oneTBB version is %s" % runtime_version()) 64 print("oneTBB interface version is %s" % runtime_interface_version()) 65 66 get_tid = lambda: threading.current_thread().ident 67 68 assert default_num_threads() == this_task_arena_max_concurrency() 69 70 def return42(): 71 return 42 72 73 def f(x): 74 return x * x 75 76 def work(mseconds): 77 res = str(mseconds) 78 if mseconds < 0: 79 mseconds = -mseconds 80 say("[%d] Start to work for %fms..." % (get_tid(), mseconds*10)) 81 time.sleep(mseconds/100.) 82 say("[%d] Work done (%fms)." % (get_tid(), mseconds*10)) 83 return res 84 85 ### Test copy/pasted from multiprocessing 86 pool = Pool(4) # start worker threads 87 88 # edge cases 89 assert pool.map(return42, []) == [] 90 assert pool.apply_async(return42, []).get() == 42 91 assert pool.apply(return42, []) == 42 92 assert list(pool.imap(return42, iter([]))) == [] 93 assert list(pool.imap_unordered(return42, iter([]))) == [] 94 assert pool.map_async(return42, []).get() == [] 95 assert list(pool.imap_async(return42, iter([])).get()) == [] 96 assert list(pool.imap_unordered_async(return42, iter([])).get()) == [] 97 98 # basic tests 99 result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously 100 assert result.get(timeout=1) == 100 # ... unless slow computer 101 assert list(pool.map(f, range(10))) == list(map(f, range(10))) 102 it = pool.imap(f, range(10)) 103 assert next(it) == 0 104 assert next(it) == 1 105 assert next(it) == 4 106 107 # Test apply_sync exceptions 108 result = pool.apply_async(time.sleep, (3,)) 109 try: 110 say(result.get(timeout=1)) # raises `TimeoutError` 111 except TimeoutError: 112 say("Good. Got expected timeout exception.") 113 else: 114 assert False, "Expected exception !" 115 assert result.get() is None # sleep() returns None 116 117 def cb(s): 118 say("Result ready: %s" % s) 119 120 # Test imap() 121 assert list(pool.imap(work, range(10, 3, -1), chunksize=4)) == list(map( 122 str, range(10, 3, -1))) 123 124 # Test imap_unordered() 125 assert sorted(pool.imap_unordered(work, range(10, 3, -1))) == sorted(map( 126 str, range(10, 3, -1))) 127 128 # Test map_async() 129 result = pool.map_async(work, range(10), callback=cb) 130 try: 131 result.get(timeout=0.01) # raises `TimeoutError` 132 except TimeoutError: 133 say("Good. Got expected timeout exception.") 134 else: 135 assert False, "Expected exception !" 136 say(result.get()) 137 138 # Test imap_async() 139 result = pool.imap_async(work, range(3, 10), callback=cb) 140 try: 141 result.get(timeout=0.01) # raises `TimeoutError` 142 except TimeoutError: 143 say("Good. Got expected timeout exception.") 144 else: 145 assert False, "Expected exception !" 146 for i in result.get(): 147 say("Item:", i) 148 say("### Loop again:") 149 for i in result.get(): 150 say("Item2:", i) 151 152 # Test imap_unordered_async() 153 result = pool.imap_unordered_async(work, range(10, 3, -1), callback=cb) 154 try: 155 say(result.get(timeout=0.01)) # raises `TimeoutError` 156 except TimeoutError: 157 say("Good. Got expected timeout exception.") 158 else: 159 assert False, "Expected exception !" 160 for i in result.get(): 161 say("Item1:", i) 162 for i in result.get(): 163 say("Item2:", i) 164 r = result.get() 165 for i in r: 166 say("Item3:", i) 167 for i in r: 168 say("Item4:", i) 169 for i in r: 170 say("Item5:", i) 171 172 # 173 # The case for the exceptions 174 # 175 176 # Exceptions in imap_unordered_async() 177 result = pool.imap_unordered_async(work, range(2, -10, -1), callback=cb) 178 time.sleep(3) 179 try: 180 for i in result.get(): 181 say("Got item:", i) 182 except (IOError, ValueError): 183 say("Good. Got expected exception") 184 185 # Exceptions in imap_async() 186 result = pool.imap_async(work, range(2, -10, -1), callback=cb) 187 time.sleep(3) 188 try: 189 for i in result.get(): 190 say("Got item:", i) 191 except (IOError, ValueError): 192 say("Good. Got expected exception") 193 194 # Stop the test: need to stop the pool !!! 195 pool.terminate() 196 pool.join() 197 198if __name__ == "__main__": 199 test() 200