Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sample(x, n, k=0):
"""Given a list `x` a sample of length ``n`` of that list is returned.
E.g. if `n` is 10, and `x` has 100 items, a list of every 10th
item is returned.
``k`` can be used as offset.
"""
j = len(x) // n
for _ in range(n):
try:
yield x[k]
except IndexError:
break
k += j
def runtest(self, fun, n=50, index=0):
with blockdetection(self.block_timeout):
t = time()
i = 0
failed = False
marker('{0}: {1}({2})'.format(index, fun.__name__, n))
try:
for i in range(n):
print('{0} ({1})'.format(i, fun.__name__), end=' ')
try:
fun()
print('-> done')
except Exception as exc:
print('-> {}'.format(exc))
except Exception:
failed = True
raise
finally:
print('{0} {1} iterations in {2}s'.format(
'failed after' if failed else 'completed',
i + 1, humanize_seconds(time() - t),
))
def run(self, names=None, iterations=50, offset=0,
numtests=None, list_all=False, repeat=0, group='all',
diag=False, no_join=False, **kw):
self.no_join = no_join
self.fbi.enable(diag)
tests = self.filtertests(group, names)[offset:numtests or None]
if list_all:
return print(self.testlist(tests))
print(self.banner(tests))
print('+ Enabling events')
self.app.control.enable_events()
it = count() if repeat == Inf else range(int(repeat) or 1)
for i in it:
marker(
'Stresstest suite start (repetition {0})'.format(i + 1),
'+',
)
for j, test in enumerate(tests):
self.runtest(test, iterations, j + 1, i + 1)
marker(
'Stresstest suite end (repetition {0})'.format(i + 1),
'+',
)
def test_iterate_simple(self):
with pytest.warns(CPendingDeprecationWarning):
it = self.ts.iterate()
results = sorted(list(it))
assert results == list(range(self.size))
def manyshort(self):
self.join(group(add.s(i, i) for i in range(1000))(),
timeout=10, propagate=True)
def test_apply(self):
self.app.conf.task_always_eager = False
from celery import chord
m = Mock()
m.app.conf.task_always_eager = False
m.AsyncResult = AsyncResult
prev, chord.run = chord.run, m
try:
x = chord(self.add.s(i, i) for i in range(10))
body = self.add.s(2)
result = x(body)
assert result.id
# does not modify original signature
with pytest.raises(KeyError):
body.options['task_id']
chord.run.assert_called()
finally:
chord.run = prev
def bigtasksbigvalue(self):
g = group(any_returning.s(BIG, sleep=0.3) for i in range(8))
r = g()
try:
self.join(r, timeout=10)
finally:
# very big values so remove results from backend
try:
r.forget()
except NotImplementedError:
pass
orig = val
if isinstance(val, _dirty):
discard_from_seen(val.objid)
continue
elif isinstance(val, _literal):
level += val.direction
yield val, it
elif isinstance(val, _key):
yield val, it
elif isinstance(val, Decimal):
yield _repr(val), it
elif isinstance(val, safe_t):
yield text_t(val), it
elif isinstance(val, chars_t):
yield _quoted(val), it
elif isinstance(val, range): # pragma: no cover
yield _repr(val), it
else:
if isinstance(val, set_t):
if not val:
yield _repr_empty_set(val), it
continue
lit_start, lit_end, val = _reprseq(
val, LIT_SET_START, LIT_SET_END, set, _chainlist,
)
elif isinstance(val, tuple):
lit_start, lit_end, val = (
LIT_TUPLE_START,
LIT_TUPLE_END_SV if len(val) == 1 else LIT_TUPLE_END,
_chainlist(val))
elif isinstance(val, dict):
lit_start, lit_end, val = (
weak (bool): The weakref state to disconnect.
dispatch_uid (Hashable): The unique identifier of the receiver
to disconnect.
"""
if weak is not None:
warnings.warn(
'Passing `weak` to disconnect has no effect.',
CDeprecationWarning, stacklevel=2)
lookup_key = _make_lookup_key(receiver, sender, dispatch_uid)
disconnected = False
with self.lock:
self._clear_dead_receivers()
for index in range(len(self.receivers)):
(r_key, _) = self.receivers[index]
if r_key == lookup_key:
disconnected = True
del self.receivers[index]
break
self.sender_receivers_cache.clear()
return disconnected
def bench_apply(n=DEFAULT_ITS):
time_start = monotonic()
task = it._get_current_object()
with app.producer_or_acquire() as producer:
[task.apply_async((i, n), producer=producer) for i in range(n)]
print('-- apply {0} tasks: {1}s'.format(n, monotonic() - time_start))