Пример #1
0
def _check_mp_mode(km, expected=False, stream="stdout"):
    execute(km=km, code="import sys")
    flush_channels(km)
    msg_id, content = execute(km=km,
                              code="print (sys.%s._check_mp_mode())" % stream)
    stdout, stderr = assemble_output(km.iopub_channel)
    nt.assert_equal(eval(stdout.strip()), expected)
Пример #2
0
def test_subprocess_print():
    """printing from forked mp.Process"""
    with new_kernel() as km:
        iopub = km.iopub_channel
        
        _check_mp_mode(km, expected=False)
        flush_channels(km)
        np = 5
        code = '\n'.join([
            "from __future__ import print_function",
            "import multiprocessing as mp",
            "pool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]" % np,
            "for p in pool: p.start()",
            "for p in pool: p.join()"
        ])
        
        expected = '\n'.join([
            "hello %s" % i for i in range(np)
        ]) + '\n'
        
        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout.count("hello"), np, stdout)
        for n in range(np):
            nt.assert_equal(stdout.count(str(n)), 1, stdout)
        nt.assert_equal(stderr, '')
        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #3
0
def test_subprocess_print():
    """printing from forked mp.Process"""
    with new_kernel() as km:
        iopub = km.iopub_channel

        _check_mp_mode(km, expected=False)
        flush_channels(km)
        np = 5
        code = '\n'.join([
            "from __future__ import print_function",
            "import multiprocessing as mp",
            "pool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]"
            % np, "for p in pool: p.start()", "for p in pool: p.join()"
        ])

        expected = '\n'.join(["hello %s" % i for i in range(np)]) + '\n'

        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout.count("hello"), np, stdout)
        for n in range(np):
            nt.assert_equal(stdout.count(str(n)), 1, stdout)
        nt.assert_equal(stderr, '')
        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #4
0
def test_simple_print():
    """simple print statement in kernel"""
    with new_kernel() as km:
        iopub = km.iopub_channel
        msg_id, content = execute(km=km, code="print ('hi')")
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, 'hi\n')
        nt.assert_equal(stderr, '')
        _check_mp_mode(km, expected=False)
    print ('hello')
Пример #5
0
def test_simple_print():
    """simple print statement in kernel"""
    with new_kernel() as km:
        iopub = km.iopub_channel
        msg_id, content = execute(km=km, code="print ('hi')")
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, 'hi\n')
        nt.assert_equal(stderr, '')
        _check_mp_mode(km, expected=False)
    print('hello')
Пример #6
0
def test_subprocess_error():
    """error in mp.Process doesn't crash"""
    with new_kernel() as km:
        iopub = km.iopub_channel
        
        code = '\n'.join([
            "import multiprocessing as mp",
            "p = mp.Process(target=int, args=('hi',))",
            "p.start()",
            "p.join()",
        ])
        
        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, '')
        nt.assert_true("ValueError" in stderr, stderr)

        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #7
0
def test_subprocess_error():
    """error in mp.Process doesn't crash"""
    with new_kernel() as km:
        iopub = km.iopub_channel

        code = '\n'.join([
            "import multiprocessing as mp",
            "p = mp.Process(target=int, args=('hi',))",
            "p.start()",
            "p.join()",
        ])

        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, '')
        nt.assert_true("ValueError" in stderr, stderr)

        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #8
0
def test_subprocess_noprint():
    """mp.Process without print doesn't trigger iostream mp_mode"""
    with new_kernel() as km:
        iopub = km.iopub_channel

        np = 5
        code = '\n'.join([
            "import multiprocessing as mp",
            "pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]" %
            np, "for p in pool: p.start()", "for p in pool: p.join()"
        ])

        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, '')
        nt.assert_equal(stderr, '')

        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #9
0
def test_subprocess_noprint():
    """mp.Process without print doesn't trigger iostream mp_mode"""
    with new_kernel() as km:
        iopub = km.iopub_channel
        
        np = 5
        code = '\n'.join([
            "import multiprocessing as mp",
            "pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]" % np,
            "for p in pool: p.start()",
            "for p in pool: p.join()"
        ])
        
        msg_id, content = execute(km=km, code=code)
        stdout, stderr = assemble_output(iopub)
        nt.assert_equal(stdout, '')
        nt.assert_equal(stderr, '')

        _check_mp_mode(km, expected=False)
        _check_mp_mode(km, expected=False, stream="stderr")
Пример #10
0
def _check_mp_mode(km, expected=False, stream="stdout"):
    execute(km=km, code="import sys")
    flush_channels(km)
    msg_id, content = execute(km=km, code="print (sys.%s._check_mp_mode())" % stream)
    stdout, stderr = assemble_output(km.iopub_channel)
    nt.assert_equal(eval(stdout.strip()), expected)