Python threading.Semaphore() Examples

The following are code examples for showing how to use threading.Semaphore(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: raveberry   Author: raveberry   File: player.py    GNU Lesser General Public License v3.0 7 votes vote down vote up
def __init__(self, musiq):
        self.SEEK_DISTANCE = 10
        self.shuffle = Setting.objects.get_or_create(key='shuffle', defaults={'value': False})[0].value == 'True'
        self.repeat = Setting.objects.get_or_create(key='repeat', defaults={'value': False})[0].value == 'True'
        self.autoplay = Setting.objects.get_or_create(key='autoplay', defaults={'value': False})[0].value == 'True'

        self.musiq = musiq
        self.queue = models.QueuedSong.objects
        Player.queue_semaphore = Semaphore(self.queue.count())
        self.alarm_playing = Event()

        self.player = mpd.MPDClient()
        self.player_lock = Lock()
        with self.mpd_command(important=True):
            self.player.clear()

        with self.mpd_command(important=True):
            status = self.player.status()
            currentsong = self.player.currentsong()
            self.volume = int(status['volume']) / 100 
Example 2
Project: AlleleHMM   Author: Danko-Lab   File: pnwsst.py    BSD 2-Clause "Simplified" License 7 votes vote down vote up
def __init__(self, port=0, interface='', daemon=True,
                 name='NwsLocalServer', logFile=None, quiet=False, **kw):

        if logFile:
            log.startLogging(open(logFile, 'w'), setStdout=False)
        elif not quiet:
            log.startLogging(sys.stderr, setStdout=False)

        if NwsLocalServer.invoked:
            raise NwsLocalServerException('I can only be invoked once!')
        NwsLocalServer.invoked = True

        threading.Thread.__init__(self, name=name, **kw)

        self._desiredPort = port
        self._interface = interface
        self.setDaemon(daemon)
        self.startSem = threading.Semaphore(0)
        self.stopSem = threading.Semaphore(0)
        self.stoppedSem = threading.Semaphore(0)
        self.start()
        self.startSem.acquire() 
Example 3
Project: Cryptchat   Author: djohsson   File: gui.py    MIT License 6 votes vote down vote up
def __init__(self, parent, nick, networkhandler):
        tkinter.Tk.__init__(self, parent)
        self.grid()
        self.title("Cryptchat")
        self.out_message = []

        self.text = TextArea(self, height=25, width=50, font=("Droid Sans Mono", 12))
        self.text.grid(row=0, column=0, sticky="NW")
        self.text.pack()

        self.entry = TextEntry(self, nick, 50, font=("Droid Sans Mono", 12))
        self.entry.grid(row=2, column=0, sticky="SW")
        self.entry.pack()

        self.out_sem = Semaphore(0)
        self.out_condition = Condition()
        self.app_condition = Condition()

        sthread = SendThread(self, networkhandler)
        rthread = ReceiveThread(self, networkhandler)
        sthread.start()
        rthread.start()

        self.resizable(width=False, height=False)
        self.mainloop() 
Example 4
Project: botbuilder-python   Author: microsoft   File: cosmosdb_storage.py    MIT License 6 votes vote down vote up
def __init__(
        self, config: CosmosDbConfig, client: cosmos_client.CosmosClient = None
    ):
        """Create the storage object.

        :param config:
        """
        super(CosmosDbStorage, self).__init__()
        self.config = config
        self.client = client or cosmos_client.CosmosClient(
            self.config.endpoint, {"masterKey": self.config.masterkey}
        )
        # these are set by the functions that check
        # the presence of the database and container or creates them
        self.database = None
        self.container = None
        self._database_creation_options = config.database_creation_options
        self._container_creation_options = config.container_creation_options
        self.__semaphore = Semaphore() 
Example 5
Project: rebel-framework   Author: reb311ion   File: Fuzzer.py    GNU General Public License v3.0 6 votes vote down vote up
def start(self):
        # Setting up testers
        self.setupScanners()
        # Setting up threads
        self.setupThreads()
        self.index = 0
        self.dictionary.reset()
        self.runningThreadsCount = len(self.threads)
        self.running = True
        self.playEvent = threading.Event()
        self.pausedSemaphore = threading.Semaphore(0)
        self.playEvent.clear()
        self.exit = False

        for thread in self.threads:
            thread.start()

        self.play() 
Example 6
Project: NiujiaoDebugger   Author: MrSrc   File: test_logging.py    GNU General Public License v3.0 6 votes vote down vote up
def setUp(self):
        """Set up a TCP server to receive log messages, and a SocketHandler
        pointing to that server's address and port."""
        BaseTest.setUp(self)
        # Issue #29177: deal with errors that happen during setup
        self.server = self.sock_hdlr = self.server_exception = None
        try:
            self.server = server = self.server_class(self.address,
                                                     self.handle_socket, 0.01)
            server.start()
            # Uncomment next line to test error recovery in setUp()
            # raise OSError('dummy error raised')
        except OSError as e:
            self.server_exception = e
            return
        server.ready.wait()
        hcls = logging.handlers.SocketHandler
        if isinstance(server.server_address, tuple):
            self.sock_hdlr = hcls('localhost', server.port)
        else:
            self.sock_hdlr = hcls(server.server_address, None)
        self.log_output = ''
        self.root_logger.removeHandler(self.root_logger.handlers[0])
        self.root_logger.addHandler(self.sock_hdlr)
        self.handled = threading.Semaphore(0) 
Example 7
Project: AlleleHMM   Author: Danko-Lab   File: pnwsst.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, port=0, interface='', daemon=True,
                 name='NwsLocalServer', logFile=None, quiet=False, **kw):

        if logFile:
            log.startLogging(open(logFile, 'w'), setStdout=False)
        elif not quiet:
            log.startLogging(sys.stderr, setStdout=False)

        if NwsLocalServer.invoked:
            raise NwsLocalServerException('I can only be invoked once!')
        NwsLocalServer.invoked = True

        threading.Thread.__init__(self, name=name, **kw)

        self._desiredPort = port
        self._interface = interface
        self.setDaemon(daemon)
        self.startSem = threading.Semaphore(0)
        self.stopSem = threading.Semaphore(0)
        self.stoppedSem = threading.Semaphore(0)
        self.start()
        self.startSem.acquire() 
Example 8
Project: ACE   Author: IntegralDefense   File: network_semaphore.py    Apache License 2.0 6 votes vote down vote up
def initialize_fallback_semaphores():
    """This needs to be called once at the very beginning of starting ACE."""

    # we need some fallback functionality for when the network semaphore server is down
    # these semaphores serve that purpose
    global_engine_instance_count = saq.CONFIG['global'].getint('global_engine_instance_count')
    for key in saq.CONFIG['network_semaphore'].keys():
        if key.startswith('semaphore_'):
            semaphore_name = key[len('semaphore_'):]
            # the configuration settings for the network semaphore specify how many connections
            # are allowed to a specific resource at once, globally
            # so if we unable to coordinate globally, the fall back is to divide the available
            # number of resources between all the engines evenly
            # that's what this next equation is for
            fallback_limit = int(floor(saq.CONFIG['network_semaphore'].getfloat(key) / float(global_engine_instance_count)))
            # we allow a minimum of one per engine
            if fallback_limit < 1:
                fallback_limit = 1

            logging.debug("fallback semaphore count for {0} is {1}".format(semaphore_name, fallback_limit))
            fallback_semaphores[semaphore_name] = LoggingSemaphore(fallback_limit)
            #fallback_semaphores[semaphore_name] = multiprocessing.Semaphore(fallback_limit)
            #fallback_semaphores[semaphore_name].semaphore_name = 'fallback {0}'.format(semaphore_name) 
Example 9
Project: cuny-bdif   Author: aristotle-tek   File: connection.py    MIT License 6 votes vote down vote up
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True, anon=False,
                 security_token=None, profile_name=None):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self.anon = anon
        self._mutex = threading.Semaphore()
        super(STSConnection, self).__init__(aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs,
                                    security_token=security_token,
                                    profile_name=profile_name) 
Example 10
Project: Yuki-Chan-The-Auto-Pentest   Author: Yukinoshita47   File: Fuzzer.py    MIT License 6 votes vote down vote up
def start(self):
        # Setting up testers
        self.setupScanners()
        # Setting up threads
        self.setupThreads()
        self.index = 0
        self.dictionary.reset()
        self.runningThreadsCount = len(self.threads)
        self.running = True
        self.playEvent = threading.Event()
        self.pausedSemaphore = threading.Semaphore(0)
        self.playEvent.clear()
        self.exit = False
        for thread in self.threads:
            thread.start()
        self.play() 
Example 11
Project: Stufts2   Author: a7a00   File: rockband.py    MIT License 6 votes vote down vote up
def main(argv):
	threads = []
	mutex = threading.Semaphore()
	turnstile = threading.Semaphore()
	turnstile2 = threading.Semaphore()
	counter = ThreadCounter()
	with open(argv[1]) as f:
		for line in f:
			threads.append(threading.Thread(target = playSounds, args = \
				(line.split(" ")[0], line.split(" ")[1], mutex, turnstile, \
					turnstile2, counter)))
	counter.setNum(len(threads))
	for i in threads:
		i.start()
	for i in threads:
		i.join() 
Example 12
Project: visual_foresight   Author: SudeepDasari   File: impedance_wsg_controller.py    MIT License 6 votes vote down vote up
def _set_gripper(self, command_pos, wait = False):
        self._desired_gpos = command_pos
        if wait:
            if self.num_timeouts > MAX_TIMEOUT:
                rospy.signal_shutdown("MORE THAN {} GRIPPER TIMEOUTS".format(MAX_TIMEOUT))

            sem = Semaphore(value=0)  # use of semaphore ensures script will block if gripper dies during execution

            self._status_mutex.acquire()
            self.sem_list.append(sem)
            self._status_mutex.release()

            start = rospy.get_time()
            self._debug_print("gripper sem acquire, list len-{}".format(len(self.sem_list)))
            sem.acquire()
            self._debug_print("waited on gripper for {} seconds".format(rospy.get_time() - start)) 
Example 13
Project: visual_foresight   Author: SudeepDasari   File: camera_recorder.py    MIT License 6 votes vote down vote up
def __init__(self, topic_name, opencv_tracking=False, save_videos=False, topic_img_dtype="bgra8"):
        self._tracking_enabled, self._save_vides = opencv_tracking, save_videos

        self._latest_image = LatestObservation(self._tracking_enabled, self._save_vides)

        self._is_tracking = False
        if self._tracking_enabled:
            self.box_height = 80

        self.bridge = CvBridge()
        self._first_status, self._status_sem = False, Semaphore(value=0)
        self._cam_height, self._cam_width = None, None
        if self._save_vides:
            self._buffers = []
            self._saving = False

        self._image_dtype = topic_img_dtype
        rospy.Subscriber(topic_name, Image_msg, self.store_latest_im)
        print('downing sema on topic: {}'.format(topic_name))
        self._status_sem.acquire()
        print("Cameras subscribed: stream is {}x{}".format(self._cam_width, self._cam_height)) 
Example 14
Project: visual_foresight   Author: SudeepDasari   File: robot_wsg_controller.py    MIT License 6 votes vote down vote up
def _set_gripper(self, command_pos, wait = False):
        self._desired_gpos = command_pos
        if wait:
            if self.num_timeouts > MAX_TIMEOUT:
                rospy.signal_shutdown("MORE THAN {} GRIPPER TIMEOUTS".format(MAX_TIMEOUT))

            sem = Semaphore(value=0)  # use of semaphore ensures script will block if gripper dies during execution

            self._status_mutex.acquire()
            self.sem_list.append(sem)
            self._status_mutex.release()

            start = rospy.get_time()
            print("gripper sem acquire, list len-{}".format(len(self.sem_list)))
            sem.acquire()
            print("waited on gripper for {} seconds".format(rospy.get_time() - start)) 
Example 15
Project: accasim   Author: cgalleguillosm   File: async_writer.py    MIT License 6 votes vote down vote up
def __init__(self, path, pre_process_fun=None, buffer_size=10000):
        """
        Constructor for the class

        :param path: Path to the output file
        :param pre_process_fun: A pre-processing function for objects pushed to the queue. It MUST be a function that
            receives an object as input, and returns a string representation of it
        """
        self._toTerminate = False
        self._thread = None
        self._deque = deque()
        self._sem = Semaphore(value=0)
        self._buffer_size = buffer_size
        self._buf_counter = 0
        if not pre_process_fun:
            pre_process_fun = self._dummy_pre_process
        self._pre_processor_wrapper = QueueFlusher(path, pre_process_fun) 
Example 16
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_logging.py    GNU General Public License v2.0 6 votes vote down vote up
def setUp(self):
        """Set up a TCP server to receive log messages, and a SocketHandler
        pointing to that server's address and port."""
        BaseTest.setUp(self)
        # Issue #29177: deal with errors that happen during setup
        self.server = self.sock_hdlr = self.server_exception = None
        try:
            self.server = server = self.server_class(self.address,
                                                     self.handle_socket, 0.01)
            server.start()
            # Uncomment next line to test error recovery in setUp()
            # raise OSError('dummy error raised')
        except OSError as e:
            self.server_exception = e
            return
        server.ready.wait()
        hcls = logging.handlers.SocketHandler
        if isinstance(server.server_address, tuple):
            self.sock_hdlr = hcls('localhost', server.port)
        else:
            self.sock_hdlr = hcls(server.server_address, None)
        self.log_output = ''
        self.root_logger.removeHandler(self.root_logger.handlers[0])
        self.root_logger.addHandler(self.sock_hdlr)
        self.handled = threading.Semaphore(0) 
Example 17
Project: blue_interface   Author: berkeleyopenarms   File: blue_interface.py    MIT License 6 votes vote down vote up
def calibrate_gripper(self):
        """Run the gripper position calibration process.
        This will automatically determine the gripper position by apply a closing
        torque and detecting when the gripper has fully closed."""

        gripper_enabled = self._gripper_enabled
        if gripper_enabled:
            self.disable_gripper()

        s = threading.Semaphore(0)

        def callback(success, values):
            s.release()

        self._calibrate_gripper_client.request({}, callback)
        s.acquire()

        if gripper_enabled:
            self.enable_gripper() 
Example 18
Project: blue_interface   Author: berkeleyopenarms   File: blue_interface.py    MIT License 6 votes vote down vote up
def _switch_controller(self, start, stop, new_control_mode=None):
        request_msg = {
            "start_controllers": start,
            "stop_controllers": stop,
            "strictness": 1  # best effort
        }

        s = threading.Semaphore(0)

        def callback(success, values):
            if success and new_control_mode is not None:
                self._control_mode = new_control_mode
            s.release()

        self._switch_controller_service_client.request(request_msg, callback)
        s.acquire()

        # Even after the controller is successfully switched, it needs a moment
        # to instantiate the command topic subscriber, etc
        time.sleep(0.01) 
Example 19
Project: pyblish-win   Author: pyblish   File: test_contextlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 20
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_driver.py    Apache License 2.0 5 votes vote down vote up
def test_attach_interface_get_config(self, mock_lock):
        """Tests that the get_config() method is properly called in
        attach_interface().
        """
        mock_lock.return_value = threading.Semaphore()

        self._test_attach_detach_interface_get_config("attach_interface") 
Example 21
Project: AshsSDK   Author: thehappydinoa   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, count):
        """A semaphore for the purpose of limiting the number of tasks

        :param count: The size of semaphore
        """
        self._semaphore = threading.Semaphore(count) 
Example 22
Project: Cryptchat   Author: djohsson   File: networkhandler.py    MIT License 5 votes vote down vote up
def __init__(self, address, port, servermode, dh):
        self.address = address
        self.port = port
        self.servermode = servermode
        self.dh = dh
        self.exchangedkeys = Event()
        self.starter = NetworkStarter(self)

        self.in_message = []
        self.in_condition = Condition()
        self.in_sem = Semaphore(0)

        self.out_message = []
        self.out_condition = Condition()
        self.out_sem = Semaphore(0) 
Example 23
Project: ngo-addons-backport   Author: camptocamp   File: web_services.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name="db"):
        netsvc.ExportService.__init__(self, name)
        self.actions = {}
        self.id = 0
        self.id_protect = threading.Semaphore() 
Example 24
Project: ngo-addons-backport   Author: camptocamp   File: web_services.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name='report'):
        netsvc.ExportService.__init__(self, name)
        self._reports = {}
        self.id = 0
        self.id_protect = threading.Semaphore() 
Example 25
Project: inmanta   Author: inmanta   File: miniapp.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.running = True
        self.lock = threading.Semaphore(0) 
Example 26
Project: RacingRobot   Author: sergionr2   File: main.py    MIT License 5 votes vote down vote up
def forceStop(command_queue, n_received_semaphore):
    """
    Stop The car
    :param command_queue: (CustomQueue) Queue for sending orders to the Arduino
    :param n_received_semaphore: (threading.Semaphore) Semaphore to regulate orders sent to the Arduino
    """
    command_queue.clear()
    n_received_semaphore.release()
    n_received_semaphore.release()
    command_queue.put((Order.MOTOR, 0))
    command_queue.put((Order.SERVO, int((THETA_MIN + THETA_MAX) / 2))) 
Example 27
Project: chattR   Author: patrickstocklin   File: synch.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 28
Project: petHomeSystem   Author: IoT-Pet-Home-System   File: DoorOperation.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.motor = 0
        self.OPEN_TERM = 10
        self.STOP_TERM = 3
        self.CLOSE_TERM = 4
        os.environ["PETHOME_DOOR_COUNT"] = "0"
        self.dp = GPIO.PWM(18, 50)
        self.setPin()
        threading.Thread.__init__(self)
        self.sem_using = threading.Semaphore(1)  # 1명만 쓸것 
Example 29
Project: petHomeSystem   Author: IoT-Pet-Home-System   File: observer.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self,push):
        self.mRQList =[]
        self.mPush =push
        self.sem_using = threading.Semaphore(1)  # 1명만 쓸것 
Example 30
Project: petHomeSystem   Author: IoT-Pet-Home-System   File: observer.py    GNU General Public License v3.0 5 votes vote down vote up
def reSet(self,push):
        self.mRQList =[]
        self.mPush =push
        self.sem_using = threading.Semaphore(1) 
Example 31
Project: acoustics-hardware   Author: AppliedAcousticsChalmers   File: core.py    MIT License 5 votes vote down vote up
def __init__(self, *args, slaves=1, **kwargs):
        super().__init__(*args, **kwargs)

        self._slave = queue.Queue()
        self._counter = threading.Semaphore(0) 
Example 32
Project: NiujiaoDebugger   Author: MrSrc   File: test_contextlib.py    GNU General Public License v3.0 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 33
Project: NiujiaoDebugger   Author: MrSrc   File: queue.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self._queue = deque()
        self._count = threading.Semaphore(0) 
Example 34
Project: AlleleHMM   Author: Danko-Lab   File: SQDedDriver.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, key, nodeFiles, ws):
        self.activeJobs = {}
        self.fileList = []
        self.key = key
        self.ws = ws
        
        LogI('Processing node files: '+str(nodeFiles))
        for f in nodeFiles:
            if f == '$PBS_NODEFILE':
                if 'PBS_NODEFILE' in os.environ:
                    global pbsFile
                    pbsFile=os.environ['PBS_NODEFILE']
                    self.fileList.append([pbsFile, os.stat(pbsFile)[8]])
                else:
                    LogE('Error, PBS_NODEFILE not defined')
                    sys.exit(1)
            else:
                self.fileList.append([f, os.stat(f)[8]])

        LogD('Filelist: %s'%self.fileList)

        self.drainedSem = threading.Semaphore(0)
        self.draining = False

        self.nodeQueue = Queue.Queue(0)
        self.nodes = {}
        self.monSem = threading.Semaphore()       # used to implement monitor-like access to an instance.
        self.resetNodePool() 
Example 35
Project: AlleleHMM   Author: Danko-Lab   File: SQDedDriver.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, key, nodeFiles, ws):
        self.activeJobs = {}
        self.fileList = []
        self.key = key
        self.ws = ws
        
        LogI('Processing node files: '+str(nodeFiles))
        for f in nodeFiles:
            if f == '$PBS_NODEFILE':
                if 'PBS_NODEFILE' in os.environ:
                    global pbsFile
                    pbsFile=os.environ['PBS_NODEFILE']
                    self.fileList.append([pbsFile, os.stat(pbsFile)[8]])
                else:
                    LogE('Error, PBS_NODEFILE not defined')
                    sys.exit(1)
            else:
                self.fileList.append([f, os.stat(f)[8]])

        LogD('Filelist: %s'%self.fileList)

        self.drainedSem = threading.Semaphore(0)
        self.draining = False

        self.nodeQueue = Queue.Queue(0)
        self.nodes = {}
        self.monSem = threading.Semaphore()       # used to implement monitor-like access to an instance.
        self.resetNodePool() 
Example 36
Project: Lyff   Author: akashlevy   File: connection.py    MIT License 5 votes vote down vote up
def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None, proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 converter=None, validate_certs=True, anon=False,
                 security_token=None, profile_name=None):
        """
        :type anon: boolean
        :param anon: If this parameter is True, the ``STSConnection`` object
            will make anonymous requests, and it will not use AWS
            Credentials or even search for AWS Credentials to make these
            requests.
        """
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint,
                                connection_cls=STSConnection)
        self.region = region
        self.anon = anon
        self._mutex = threading.Semaphore()
        provider = 'aws'
        # If an anonymous request is sent, do not try to look for credentials.
        # So we pass in dummy values for the access key id, secret access
        # key, and session token. It does not matter that they are
        # not actual values because the request is anonymous.
        if self.anon:
            provider = Provider('aws', NO_CREDENTIALS_PROVIDED,
                                NO_CREDENTIALS_PROVIDED,
                                NO_CREDENTIALS_PROVIDED)
        super(STSConnection, self).__init__(aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    validate_certs=validate_certs,
                                    security_token=security_token,
                                    profile_name=profile_name,
                                    provider=provider) 
Example 37
Project: OSCAR   Author: Xaxetrov   File: shared_objects.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        # Semaphore unlocked when the observation are set
        self.semaphore_obs_ready = threading.Semaphore(value=0)
        # Semaphore unlocked when the action is set
        self.semaphore_action_set = threading.Semaphore(value=0)
        # shared memory containing the action that must be done by the env
        self.shared_action = None
        # shared memory containing the observation get by the env
        self.shared_obs = None
        # number of possible action (set by the env)
        self.action_space = None
        # shape of the observations given by the env
        self.observation_space = None
        # vector masking defining the action that are available or not
        self.available_action_mask = None 
Example 38
Project: aws-cfn-plex   Author: lordmuffin   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, count):
        """A semaphore for the purpose of limiting the number of tasks

        :param count: The size of semaphore
        """
        self._semaphore = threading.Semaphore(count) 
Example 39
Project: bamf   Author: malwaredllc   File: bamf.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, shodan_api=None):
        """
        Initialize a new Bamf instance

        `Optional`
        :param str shodan_api:  Shodan API key

        """
        mechanize.Browser.__init__(self)
        self._models = {}
        self._targets = {}
        self._devices = []
        self._threads = []
        self._backdoors = []
        self._queue = Queue.Queue()
        self._query = 'alphanetworks/2.23'
        self._ports = [8000, 8080, 8888]
        self._semaphore = threading.Semaphore(value=1)
        self._database = sqlite3.connect('database.db')
        self._database.executescript(self.__tbl_config)
        self._database.executescript(self.__tbl_routers)
        self._database.executescript(self.__tbl_devices)
        self._shodan = self._init_shodan(shodan_api)
        self.addheaders = [('User-Agent', 'xmlset_roodkcableoj28840ybtide')]
        self.set_handle_robots(False)
        self.set_handle_redirect(True)
        self.set_handle_refresh(True)
        self.set_handle_equiv(True)
        self.set_handle_referer(True)
        self.set_debug_http(False)
        self.set_debug_responses(False) 
Example 40
Project: nova   Author: ZhanHan   File: test_driver.py    Apache License 2.0 5 votes vote down vote up
def test_attach_interface_get_config(self, mock_lock):
        """Tests that the get_config() method is properly called in
        attach_interface().
        """
        mock_lock.return_value = threading.Semaphore()

        self._test_attach_detach_interface_get_config("attach_interface") 
Example 41
Project: grand_central   Author: amiracle   File: utils.py    MIT License 5 votes vote down vote up
def __init__(self, count):
        """A semaphore for the purpose of limiting the number of tasks

        :param count: The size of semaphore
        """
        self._semaphore = threading.Semaphore(count) 
Example 42
Project: TESTGIT   Author: B-ROY   File: synch.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.mutex     = threading.RLock()
        self.can_read  = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers  = 0
        self.active_writers  = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 43
Project: AneMo   Author: jspargo   File: synch.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 44
Project: mars   Author: mars-project   File: executor.py    Apache License 2.0 5 votes vote down vote up
def semaphore(cls, value):
        return threading.Semaphore(value) 
Example 45
Project: mars   Author: mars-project   File: executor.py    Apache License 2.0 5 votes vote down vote up
def semaphore(cls, value):
        # as gevent threadpool is the **real** thread, so use threading.Semaphore
        return threading.Semaphore(value) 
Example 46
Project: oa_qian   Author: sunqb   File: compat.py    Apache License 2.0 5 votes vote down vote up
def __exit__(self, t, v, tb):
            self.release()
    # -- end backport of Semaphore from Python 3.4 -- # 
Example 47
Project: pysequen   Author: mikecoop83   File: blockingdeque.py    MIT License 5 votes vote down vote up
def __init__(self, maxlen):
        self.sema = Semaphore(maxlen)
        self.items = list() 
Example 48
Project: lazypool   Author: natw   File: executors.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, num_workers=1):
        self.num_workers = num_workers
        self.result_queue = Queue()
        self.thread_sem = threading.Semaphore(num_workers)
        self._shutdown = threading.Event()
        self.threads = [] 
Example 49
Project: GTDWeb   Author: lanbing510   File: synch.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 50
Project: liberator   Author: libscie   File: synch.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 51
Project: ironpython2   Author: IronLanguages   File: test_contextlib.py    Apache License 2.0 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 52
Project: iv-issue-finder   Author: d-Rickyy-b   File: threadpool.py    MIT License 5 votes vote down vote up
def __init__(self, workers):
        self.workers = workers
        self.semaphore = Semaphore(workers)
        self.threads = []
        self.stop_event = Event()
        self.stop_event.clear() 
Example 53
Project: respeaker_python_library   Author: respeaker   File: pyusb_backend.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(PyUSB, self).__init__()
        self.ep_out = None
        self.ep_in = None
        self.dev = None
        self.closed = False
        self.rcv_data = []
        self.read_sem = threading.Semaphore(0) 
Example 54
Project: sublime-win-bak   Author: okoala   File: PyV8.py    MIT License 5 votes vote down vote up
def testMultiPythonThread(self):
        import time, threading

        class Global:
            count = 0
            started = threading.Event()
            finished = threading.Semaphore(0)

            def sleep(self, ms):
                time.sleep(ms / 1000.0)

                self.count += 1

        g = Global()

        def run():
            with JSContext(g) as ctxt:
                ctxt.eval("""
                    started.wait();

                    for (i=0; i<10; i++)
                    {
                        sleep(100);
                    }

                    finished.release();
                """)

        threading.Thread(target=run).start()

        now = time.time()

        self.assertEqual(0, g.count)

        g.started.set()
        g.finished.acquire()

        self.assertEqual(10, g.count)

        self.assertTrue((time.time() - now) >= 1) 
Example 55
Project: sublime-win-bak   Author: okoala   File: PyV8.py    MIT License 5 votes vote down vote up
def testMultiPythonThread(self):
        import time, threading

        class Global:
            count = 0
            started = threading.Event()
            finished = threading.Semaphore(0)

            def sleep(self, ms):
                time.sleep(ms / 1000.0)

                self.count += 1

        g = Global()

        def run():
            with JSContext(g) as ctxt:
                ctxt.eval("""
                    started.wait();

                    for (i=0; i<10; i++)
                    {
                        sleep(100);
                    }

                    finished.release();
                """)

        threading.Thread(target=run).start()

        now = time.time()

        self.assertEqual(0, g.count)

        g.started.set()
        g.finished.acquire()

        self.assertEqual(10, g.count)

        self.assertTrue((time.time() - now) >= 1) 
Example 56
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_contextlib.py    GNU General Public License v3.0 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 57
Project: Fun5G   Author: OpenWinCon   File: synch.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 58
Project: py_admin   Author: leeyisoft   File: object.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 59
Project: visual_foresight   Author: SudeepDasari   File: impedance_wsg_controller.py    MIT License 5 votes vote down vote up
def __init__(self, control_rate, robot_name, print_debug):
        self.max_release = 0
        self._print_debug = print_debug
        RobotController.__init__(self)
        self.sem_list = [Semaphore(value = 0)]
        self._status_mutex = Lock()
        self.robot_name = robot_name

        self._desired_gpos = GRIPPER_OPEN
        self.gripper_speed = 300

        self._force_counter = 0
        self._integrate_gripper_force, self._last_integrate = 0., None
        self.num_timeouts = 0

        self._cmd_publisher = rospy.Publisher('/robot/limb/right/joint_command', JointCommand, queue_size=100)
        self.gripper_pub = rospy.Publisher('/wsg_50_driver/goal_position', Cmd, queue_size=10)
        rospy.Subscriber("/wsg_50_driver/status", Status, self._gripper_callback)

        print("waiting for first status")
        self.sem_list[0].acquire()
        print('gripper initialized!')

        self.control_rate = rospy.Rate(control_rate)

        self._navigator = intera_interface.Navigator()
        self._navigator.register_callback(self._close_gripper_handler, 'right_button_ok') 
Example 60
Project: visual_foresight   Author: SudeepDasari   File: robot_wsg_controller.py    MIT License 5 votes vote down vote up
def __init__(self, control_rate, robot_name):
        self.max_release = 0
        RobotController.__init__(self)
        self.sem_list = [Semaphore(value = 0)]
        self._status_mutex = Lock()
        self.robot_name = robot_name

        self._desired_gpos = GRIPPER_OPEN
        self.gripper_speed = 300

        self._force_counter = 0
        self._integrate_gripper_force = 0.
        self.num_timeouts = 0

        self.gripper_pub = rospy.Publisher('/wsg_50_driver/goal_position', Cmd, queue_size=10)
        rospy.Subscriber("/wsg_50_driver/status", Status, self._gripper_callback)

        print("waiting for first status")
        self.sem_list[0].acquire()
        print('gripper initialized!')

        self.imp_ctrl_publisher = rospy.Publisher('/desired_joint_pos', JointState, queue_size=1)
        self.imp_ctrl_release_spring_pub = rospy.Publisher('/release_spring', Float32, queue_size=10)
        self.imp_ctrl_active = rospy.Publisher('/imp_ctrl_active', Int64, queue_size=10)

        self.control_rate = rospy.Rate(control_rate)

        self.imp_ctrl_release_spring(100)
        self.imp_ctrl_active.publish(1)

        self._navigator = intera_interface.Navigator()
        self._navigator.register_callback(self._close_gripper_handler, 'right_button_ok') 
Example 61
Project: xenascriptlibs   Author: xenadevel   File: TestUtilsL47.py    MIT License 5 votes vote down vote up
def __init__(self, hostname, port = 22611):
        SimpleSocket.__init__(self, hostname = hostname, port = port)
        SimpleSocket.set_keepalives(self)
        self.access_semaphor = threading.Semaphore(1) 
Example 62
Project: xenascriptlibs   Author: xenadevel   File: TestUtilsL23.py    MIT License 5 votes vote down vote up
def __init__(self, hostname, port = 22611):
        SimpleSocket.__init__(self, hostname = hostname, port = port)
        SimpleSocket.set_keepalives(self)
        self.access_semaphor = threading.Semaphore(1) 
Example 63
Project: deepstacks   Author: guoxuesong   File: using_videodir.py    MIT License 5 votes vote down vote up
def filter_notbusy(vs):
    vres=[]
    cres=[0]
    for v in vs:
        #v.sync()
        if not v.busy:
            vres+=[v]
            cres+=[cres[-1]+(v.endpos-v.beginpos)]
    cres=cres[1:]
    return vres,cres

#semaphore = threading.Semaphore(3) 
Example 64
Project: ghidra_bridge   Author: justfoxing   File: bridge.py    MIT License 5 votes vote down vote up
def __init__(self, bridge_conn):
        self.thread_count = 0
        self.bridge_conn = bridge_conn
        self.ready_threads = threading.Semaphore(
            0)  # start the ready threads at 0
        self.command_list = list()
        self.command_list_read_lock = threading.Lock()
        self.command_list_write_lock = threading.Lock() 
Example 65
Project: python-gui   Author: neovim   File: ui_bridge.py    Apache License 2.0 5 votes vote down vote up
def connect(self, nvim, ui, profile=None, notify=False):
        """Connect nvim and the ui.

        This will start loops for handling the UI and nvim events while
        also synchronizing both.
        """
        self._notify = notify
        self._error = None
        self._nvim = nvim
        self._ui = ui
        self._profile = profile
        self._sem = Semaphore(0)
        debug_env = os.environ.get("NVIM_PYTHON_UI_DEBUG", "")
        debug_ext_env = os.environ.get("NVIM_PYTHON_UI_DEBUG_EXT", "")
        self.debug_ext = len(debug_ext_env) > 0
        if debug_env == "2":
            self.debug_events = 2
        else:
            self.debug_events = len(debug_env) > 0 or self.debug_ext
        t = Thread(target=self._nvim_event_loop)
        t.daemon = True
        t.start()
        self._ui_event_loop()
        if self._error:
            print(self._error)
        if self._profile:
            print(self._profile) 
Example 66
Project: Vigtech-Services   Author: VigTech   File: synch.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 67
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_contextlib.py    GNU General Public License v2.0 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 68
Project: DroidPot   Author: WhySoGeeky   File: synch.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 69
Project: PopularityBased-SearchEngine   Author: rsunder10   File: synch.py    MIT License 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 70
Project: oss-ftp   Author: aliyun   File: test_contextlib.py    MIT License 5 votes vote down vote up
def testWithSemaphore(self):
        lock = threading.Semaphore()
        def locked():
            if lock.acquire(False):
                lock.release()
                return False
            else:
                return True
        self.boilerPlate(lock, locked) 
Example 71
Project: mudong_blog   Author: mudong1991   File: synch.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        self.mutex = threading.RLock()
        self.can_read = threading.Semaphore(0)
        self.can_write = threading.Semaphore(0)
        self.active_readers = 0
        self.active_writers = 0
        self.waiting_readers = 0
        self.waiting_writers = 0 
Example 72
Project: blue_interface   Author: berkeleyopenarms   File: blue_interface.py    MIT License 5 votes vote down vote up
def command_gripper(self, position, effort, wait=False):
        # TODO: change robot-side so position and effort in correct units
        """Send a goal to gripper, and optionally wait for the goal to be reached.

        Args:
            position (float64): gap size between gripper fingers in cm.
            effort (float64): maximum effort the gripper with exert before
                stalling in N.
        """

        if not self._gripper_enabled:
            self.enable_gripper()

        goal_msg = {"command": {
            "position": position,
            "max_effort": effort
        }}

        s = threading.Semaphore(0)

        def callback(result, status):
            if result["stalled"] or result["reached_goal"]:
                s.release()

        self._gripper_goal_id = self._gripper_action_client.send_goal(
            goal_msg, callback, callback)
        if wait:
            s.acquire() 
Example 73
Project: blue_interface   Author: berkeleyopenarms   File: blue_interface.py    MIT License 5 votes vote down vote up
def _load_controller(self, name):
        request_msg = {
            "name": name
        }

        s = threading.Semaphore(0)

        def callback(success, values):
            s.release()

        self._load_controller_service_client.request(request_msg, callback)
        s.acquire() 
Example 74
Project: robust-atd   Author: shadowbq   File: __init__.py    MIT License 4 votes vote down vote up
def __init__(self, options):

        self.options = options
        self.path = options.directory
        self.temp_dir = tempfile.mkdtemp()
        self.i = 0
        number_of_threads = int(self.options.maxthreads)
        self.pool = ActivePool()
        self.s = threading.Semaphore(number_of_threads)

        # self.event_handler = watchdog.events.PatternMatchingEventHandler(
        #   patterns=["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.pdf"],
        #   ignore_patterns=[],
        #   ignore_directories=True)

        if self.options.existing:
            if self.options.verbosity:
                print("Parsing Existing")
            full_file_paths = self.get_filepaths(self.path)
            self.i = 0
            for file_name in full_file_paths:
                if self.options.verbosity:
                    print("Existing file => {}".format(file_name))
                self.i = self.i + 1
                self.options.file_to_upload = file_name
                t = threading.Thread(
                    target=worker,
                    name=str(self.i),
                    args=(
                        self.s,
                        self.pool,
                        self.options,
                        self.options.file_to_upload
                    )
                )
                t.start()

        if self.options.follow:
            if self.options.verbosity:
                print("Starting Watchdog handlers")
            self.event_handler = watchdog.events.FileSystemEventHandler()
            # Thread handler
            self.event_handler.on_created = self.on_created
            self.observer = Observer()
            self.observer.schedule(self.event_handler, self.path, recursive=True)
            self.observer.start() 
Example 75
Project: git-aggregator   Author: acsone   File: main.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def run(args):
    """Load YAML and JSON configs and run the command specified
    in args.command"""

    repos = load_config(args.config, args.expand_env, args.force)

    jobs = max(args.jobs, 1)
    threads = []
    sem = threading.Semaphore(jobs)
    err_queue = Queue()

    for repo_dict in repos:
        if not err_queue.empty():
            break

        sem.acquire()
        r = Repo(**repo_dict)
        tname = os.path.basename(repo_dict['cwd'])

        if jobs > 1:
            t = threading.Thread(
                target=aggregate_repo, args=(r, args, sem, err_queue))
            t.daemon = True
            t.name = tname
            threads.append(t)
            t.start()
        else:
            with ThreadNameKeeper():
                threading.current_thread().name = tname
                aggregate_repo(r, args, sem, err_queue)

    for t in threads:
        t.join()

    if not err_queue.empty():
        while True:
            try:
                exc_type, exc_obj, exc_trace = err_queue.get_nowait()
            except EmptyQueue:
                break
            traceback.print_exception(exc_type, exc_obj, exc_trace)
        sys.exit(1) 
Example 76
Project: pywhdfs   Author: yassineazzouz   File: client.py    MIT License 4 votes vote down vote up
def __init__(
           self,
           nameservices,
           max_concurrency=-1,
           pool_connections=60,
           root=None,
           proxy=None,
           timeout=None,
           verify=False,
           truststore=None,
           session=None
       ):

    # Comma separed list of namenodes urls
    self.host_list = SyncHostsList(nameservices)
    self.root = root

    self.max_fail_retries = 10
    self.on_fail_delay = 6 # Seconds

    self._session = session or rq.Session()
    # Use a bigger connection pool due to the big number of concurrent threads 
    adapter = rq.adapters.HTTPAdapter(max_retries=5, pool_connections=pool_connections, pool_maxsize=pool_connections)
    self._session.mount('http://', adapter)
    self._session.mount('https://', adapter)

    self.max_concurrency = int(max_concurrency)
    if self.max_concurrency > 0:
      self._lock = Lock()
      self._sem = Semaphore(int(self.max_concurrency))
      # ensure there is a least _concurency_delay time difference between
      # two consecutive requests, avoid flooding the namenode/datanode with requests
      self._concurency_delay = 0.001 # Seconds.
      self._timestamp = time.time() - self._concurency_delay

    if proxy:
      if not self._session.params:
        self._session.params = {}
      self._session.params['doas'] = proxy

    self._timeout = timeout
    self.proxy = proxy

    if verify and truststore is not None:
      self._verify = truststore
      _logger.info('Using secure connection with truststore %r', truststore)
    else:
      self._verify = verify

    _logger.info('Instantiated %r.', self) 
Example 77
Project: DBUtils   Author: Cito   File: PooledPg.py    MIT License 4 votes vote down vote up
def __init__(
            self, mincached=0, maxcached=0,
            maxconnections=0, blocking=False,
            maxusage=None, setsession=None, reset=None,
            *args, **kwargs):
        """Set up the PostgreSQL connection pool.

        mincached: initial number of connections in the pool
            (0 means no connections are made at startup)
        maxcached: maximum number of connections in the pool
            (0 or None means unlimited pool size)
        maxconnections: maximum number of connections generally allowed
            (0 or None means an arbitrary number of connections)
        blocking: determines behavior when exceeding the maximum
            (if this is set to true, block and wait until the number of
            connections decreases, otherwise an error will be reported)
        maxusage: maximum number of reuses of a single connection
            (0 or None means unlimited reuse)
            When this maximum usage number of the connection is reached,
            the connection is automatically reset (closed and reopened).
        setsession: optional list of SQL commands that may serve to prepare
            the session, e.g. ["set datestyle to ...", "set time zone ..."]
        reset: how connections should be reset when returned to the pool
            (0 or None to rollback transcations started with begin(),
            1 to always issue a rollback, 2 for a complete reset)
        args, kwargs: the parameters that shall be used to establish
            the PostgreSQL connections using class PyGreSQL pg.DB()

        """
        self._args, self._kwargs = args, kwargs
        self._maxusage = maxusage
        self._setsession = setsession
        self._reset = reset or 0
        if mincached is None:
            mincached = 0
        if maxcached is None:
            maxcached = 0
        if maxconnections is None:
            maxconnections = 0
        if maxcached:
            if maxcached < mincached:
                maxcached = mincached
        if maxconnections:
            if maxconnections < maxcached:
                maxconnections = maxcached
            # Create semaphore for number of allowed connections generally:
            from threading import Semaphore
            self._connections = Semaphore(maxconnections)
            self._blocking = blocking
        else:
            self._connections = None
        self._cache = Queue(maxcached)  # the actual connection pool
        # Establish an initial number of database connections:
        idle = [self.connection() for i in range(mincached)]
        while idle:
            idle.pop().close() 
Example 78
Project: ACE   Author: IntegralDefense   File: network_semaphore.py    Apache License 2.0 4 votes vote down vote up
def __init__(self):
        # set to True to gracefully shutdown
        self.shutdown = False
    
        # the main thread that listens for new connections
        self.server_thread = None

        # the main listening socket
        self.server_socket = None

        # configuration settings
        if 'network_semaphore' not in saq.CONFIG:
            logging.error("missing configuration network_semaphore")
            sys.exit(1)

        self.config = saq.CONFIG['network_semaphore']
        
        # binding address
        self.bind_address = self.config['bind_address']
        self.bind_port = self.config.getint('bind_port')

        # source IP addresses that are allowed to connect
        self.allowed_ipv4 = [ipaddress.ip_network(x.strip()) for x in self.config['allowed_ipv4'].split(',')]

        # load and initialize all the semaphores we're going to use
        self.semaphores = {} # key = semaphore_name, value = Semaphore
        for key in self.config.keys():
            if key.startswith('semaphore_'):
                semaphore_name = key[len('semaphore_'):]
                count = self.config.getint(key)
                self.semaphores[semaphore_name] = LoggingSemaphore(count)
                self.semaphores[semaphore_name].semaphore_name = semaphore_name # lol
                logging.debug("loaded semaphore {0} with capacity {1}".format(semaphore_name, count))

        # we keep some stats and metrics on semaphores in this directory
        self.stats_dir = os.path.join(saq.DATA_DIR, self.config['stats_dir'])
        if not os.path.isdir(self.stats_dir):
            try:
                os.makedirs(self.stats_dir)
            except Exception as e:
                logging.error("unable to create directory {0}: {1}".format(self.stats_dir, str(e)))
                sys.exit(1)

        # a thread monitors and records statistics
        self.monitor_thread = None 
Example 79
Project: papageorge   Author: drestebon   File: cli.py    GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, fics_pass):
        self.fics_pass = fics_pass
        self.TEXT_RE = [
            ( # forward backward - DROP
              re.compile('^Game \w+: \w+ (goes forward|backs up)'),
                lambda regexp, txt: False),
            ( re.compile('^<s[cr]*>'),
                self.fics_update_seek_graph),
            ( re.compile('^<12>'),
                self.fics_style12),
            ( re.compile('^<g1>'),
                self.fics_game_info),
            ( re.compile('^{Game (\d+) .+}( [012/-]+)?'),
                self.fics_interruptus),
            ( re.compile('^Challenge:'),
                self.fics_challenge),
            ( re.compile('^(?P<opponent>\w{3,}) (offers|would|requests)'),
                self.fics_offer),
            ( re.compile('^You are no longer examining game (\d+)'),
                self.fics_unexamine),
            ( re.compile('^Removing game (\d+) from observation list.'),
                self.fics_unexamine),
            ( re.compile('^\\\\\s+(.+)'),
                self.continuation),
            ( re.compile('^fics% ((\s|.|\n)+)'),
                self.strip_prompt),
        ]
        for restring, hcolor in config.console.highlight:
            if hcolor == 'palette':
                self.TEXT_RE.insert(0, (re.compile(restring),
                    lambda regexp, txt, hcolor=hcolor: (
                        urwid.AttrSpec(self.palette(regexp.group('id')),
                            'default'), txt)))
            else:
                self.TEXT_RE.insert(0, (re.compile(restring),
                    lambda regexp, txt, hcolor=hcolor: (urwid.AttrSpec(hcolor,
                                                            'default'), txt)))
        self.re_rules()
        self._palette = list()
        for hcolor in config.console.palette:
            self._palette.append( (hcolor, list()) )
        self.body_size = None
        self.die = 0
        self.txt_list = urwid.ListBox(urwid.SimpleFocusListWalker(
                                                    [ConsoleText('')]))
        self.cmd_line = CmdLine('> ')
        self.cmd_line.menu_placeholder = urwid.WidgetPlaceholder(urwid.Pile([]))
        bottom = urwid.WidgetDisable(self.cmd_line.menu_placeholder)
        bottom = urwid.Pile([bottom, self.cmd_line])
        self._wait_for_sem = threading.Semaphore(0)
        self._wait_for_txt = None
        self._wait_for_gn = None
        self._wait_for_buf = list()
        self._cancel_moves = False
        self.ML_recording = False
        self._last_AB = None
        self.handle_commands = None
        self.temp_buff = None
        hdr = urwid.Text((urwid.AttrSpec('#999, standout', '#111'), " Console "))
        return super(CLI, self).__init__(self.txt_list,
                        header=None, footer=bottom, focus_part='footer') 
Example 80
Project: blue_interface   Author: berkeleyopenarms   File: blue_interface.py    MIT License 4 votes vote down vote up
def inverse_kinematics(self, position, orientation,
                           seed_joint_positions=[]):
        """Given a desired cartesian pose for the end effector, compute the
        necessary joint angles. Note that the system is underparameterized and
        there are an infinite number of possible solutions; this will only
        return a single possible one.

        Args:
            position (iterable): A length-3 array containing a cartesian position
                (x,y,z), wrt the world frame.
            orientation (iterable): A length-4 array containing a quaternion
                (x,y,z,w), wrt the world frame.
            seed_joint_positions (iterable, optional): An array of 7 joint
                angles, to be used to initalize the IK solver.
        Returns:
            numpy.ndarray: An array of 7 joint angles, or an empty array if no
            solution was found.
        """

        output = []
        s = threading.Semaphore(0)

        def callback(success, values):
            if success:
                output.extend(values["ik_joint_positions"])
            s.release()

        request_msg = {
            "end_effector_pose": {
                "header": {
                    "frame_id": self._WORLD_FRAME
                },
                "pose": {
                    "position": dict(zip("xyz", position)),
                    "orientation": dict(zip("xyzw", orientation))
                }
            },
            "solver": "trac-ik",
            "seed_joint_positions": seed_joint_positions
        }
        self._inverse_kinematics_client.request(request_msg, callback)
        s.acquire()

        return np.asarray(output)