Exemplo n.º 1
0
    def basic_publish (self,
                       payload,
                       exchange='',
                       routing_key='',
                       mandatory=False,
                       immediate=False,
                       properties=None):

        if self.confirm_mode:
            self._next_delivery_tag += 1
            dtag = self._next_delivery_tag
            assert dtag not in self._pending_published
            s = coro.inverted_semaphore(1)
            self._pending_published[dtag] = s
            #W('basic_publish pending >>> %d\n' % dtag);
            self._basic_publish(payload,
                                exchange,
                                routing_key,
                                mandatory,
                                immediate,
                                properties)
            self._publish_cv.wake_one()
            s.block_till_zero()
            #W('basic_publish pending <<< %d\n' % dtag);
        else:
            self._basic_publish(payload,
                                exchange,
                                routing_key,
                                mandatory,
                                immediate,
                                properties)
Exemplo n.º 2
0
 def test_isem_schedule_interrupt(self):
     """Test schedule then interrupt on inverted semaphore."""
     s = coro.inverted_semaphore()
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._isem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._isem_block, s)
     coro.yield_slice()
     # Schedule all of the threads.
     s.release(1)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemplo n.º 3
0
 def test_isem_schedule_interrupt(self):
     """Test schedule then interrupt on inverted semaphore."""
     s = coro.inverted_semaphore()
     s.acquire(1)
     self._resume_count = 0
     threads = []
     # Spawn some threads that will block and be interrupted.
     for unused in xrange(5):
         threads.append(coro.spawn(self._isem_block, s))
     # Spawn a thread that we will not interrupt.
     no_interrupt_thread = coro.spawn(self._isem_block, s)
     coro.yield_slice()
     # Schedule all of the threads.
     s.release(1)
     # Now interrupt them.
     for t in threads:
         t.shutdown()
     coro.yield_slice()
     # Verify that it ran.
     self.assertEqual(self._resume_count, 1)
Exemplo n.º 4
0
    def __init__(self):
        self._worker_semaphore = coro.inverted_semaphore()
        self._writes_finished = 0
        self._reads_finished = 0
        self._bytes_written = 0
        self._bytes_read = 0
        self._start_time = 0
        self._fd = -1
        self._written = []
        self._size = 0
        self._num_live_writers = 0

        self._write_cancel_success = 0
        self._write_cancel_fail = 0
        self._read_cancel_success = 0
        self._read_cancel_fail = 0
        self._assertion_errors = 0
        self._write_locks = None
        self._read_locks = None

        self.main_thread_state = 'Not started.'
        self.writer_status = {}
        self.reader_status = {}
        self.lio_status = {}
Exemplo n.º 5
0
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

l = coro.inverted_semaphore()


def worker(sleep):
    global l
    print 'working starting'
    l.acquire()
    coro.sleep_relative(sleep)
    print 'worker done, releasing'
    l.release()


def waiter():
    global l
    print 'waiter'
    l.block_till_zero()
Exemplo n.º 6
0
def capture_with_stderr(command, cwd=None, env=None, timeout=0, pgrp=0):
    """Run a program in the background and capture its output.

    stdout and stderr are captured independently.

    :Parameters:
        - `command`: The command to execute.  If it is a string, it will be
          parsed for command-line arguments.  Otherwise it assumes it is a
          sequence of arguments, with the first element being the command to
          execute.

          If the command does not contain a slash (/) it will search the PATH
          environment for the executable.
        - `cwd`: Change the working directory to this path if specified before
          executing the program.
        - `env`: The environment to use.  If None, the environment is not
          changed.  May be a dictionary or a list of 'NAME=VALUE' strings.
        - `timeout`: If specified, will use a coro timeout to ensure that the
          process returns within the specified length of time.  If it does not,
          it is forcefully killed (with SIGKILL) and `ProcessTimeout` is
          raised.
        - `pgrp`: Set to -1 to keep process group unchanged, 0 to create a new
          job (default) and >0 to set process group to pgrp

    :Return:
        Returns a tuple ``(status, stdout_output, stderr_output)``.  Status is an
        `ExitStatus` instance.  The outputs are strings.

    :Exceptions:
        - `OSError`: Generic system error.
        - `ValueError`: The command value is invalid.
        - `ProcessTimeout`: The process did not return within `timeout`
          seconds.
    """
    status = None
    stdout_result = []
    stderr_result = []

    finished_sem = coro.inverted_semaphore(2)

    p = spawn_job_bg(command, stdin=DEV_NULL, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env, pgrp=pgrp)

    def do_read(s, result):
        while 1:
            block = s.read(1024)
            if block:
                result.append(block)
            else:
                break
        finished_sem.release()

    def do_work():
        finished_sem.block_till_zero()
        return p.wait()

    try:
        coro.spawn(do_read, p.stdout, stdout_result)
        coro.spawn(do_read, p.stderr, stderr_result)
        if timeout:
            status = coro.with_timeout(timeout, do_work)
        else:
            status = do_work()
    except BaseException, e:
        try:
            p.killpg(signal.SIGKILL)
        except OSError, kill_exc:
            if kill_exc.errno != errno.ESRCH:
                raise
Exemplo n.º 7
0
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import coro

l = coro.inverted_semaphore()

def worker(sleep):
    global l
    print 'working starting'
    l.acquire()
    coro.sleep_relative(sleep)
    print 'worker done, releasing'
    l.release()

def waiter():
    global l
    print 'waiter'
    l.block_till_zero()
    print 'waiter awoke'
    coro._exit = 1
Exemplo n.º 8
0
def capture_with_stderr(command, cwd=None, env=None, timeout=0, pgrp=0):
    """Run a program in the background and capture its output.

    stdout and stderr are captured independently.

    :Parameters:
        - `command`: The command to execute.  If it is a string, it will be
          parsed for command-line arguments.  Otherwise it assumes it is a
          sequence of arguments, with the first element being the command to
          execute.

          If the command does not contain a slash (/) it will search the PATH
          environment for the executable.
        - `cwd`: Change the working directory to this path if specified before
          executing the program.
        - `env`: The environment to use.  If None, the environment is not
          changed.  May be a dictionary or a list of 'NAME=VALUE' strings.
        - `timeout`: If specified, will use a coro timeout to ensure that the
          process returns within the specified length of time.  If it does not,
          it is forcefully killed (with SIGKILL) and `ProcessTimeout` is
          raised.
        - `pgrp`: Set to -1 to keep process group unchanged, 0 to create a new
          job (default) and >0 to set process group to pgrp

    :Return:
        Returns a tuple ``(status, stdout_output, stderr_output)``.  Status is an
        `ExitStatus` instance.  The outputs are strings.

    :Exceptions:
        - `OSError`: Generic system error.
        - `ValueError`: The command value is invalid.
        - `ProcessTimeout`: The process did not return within `timeout`
          seconds.
    """
    status = None
    stdout_result = []
    stderr_result = []

    finished_sem = coro.inverted_semaphore(2)

    p = spawn_job_bg(command, stdin=DEV_NULL, stdout=PIPE, stderr=PIPE, cwd=cwd, env=env, pgrp=pgrp)

    def do_read(s, result):
        while 1:
            block = s.read(1024)
            if block:
                result.append(block)
            else:
                break
        finished_sem.release()

    def do_work():
        finished_sem.block_till_zero()
        return p.wait()

    try:
        coro.spawn(do_read, p.stdout, stdout_result)
        coro.spawn(do_read, p.stderr, stderr_result)
        if timeout:
            status = coro.with_timeout(timeout, do_work)
        else:
            status = do_work()
    except BaseException, e:
        try:
            p.killpg(signal.SIGKILL)
        except OSError, kill_exc:
            if kill_exc.errno != errno.ESRCH:
                raise