tasq-client-python 0.1.13__tar.gz → 0.1.15__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tasq-client-python
3
- Version: 0.1.13
3
+ Version: 0.1.15
4
+ Requires-Dist: requests
@@ -4,5 +4,5 @@ setup(
4
4
  name="tasq-client-python",
5
5
  packages=["tasq_client"],
6
6
  install_requires=["requests"],
7
- version="0.1.13",
7
+ version="0.1.15",
8
8
  )
@@ -1,11 +1,11 @@
1
- import multiprocessing
2
1
  import random
3
2
  import sys
4
3
  import time
5
4
  import urllib.parse
6
5
  from contextlib import contextmanager
7
6
  from dataclasses import dataclass
8
- from multiprocessing.context import BaseContext
7
+ from queue import Empty, Queue
8
+ from threading import Thread
9
9
  from typing import Any, Dict, List, Optional, Tuple
10
10
 
11
11
  import requests
@@ -35,6 +35,8 @@ class QueueCounts:
35
35
  # server is old enough to not support rate estimation.
36
36
  rate: Optional[float] = None
37
37
 
38
+ modtime: Optional[int] = None
39
+
38
40
 
39
41
  class TasqClient:
40
42
  """
@@ -82,7 +84,6 @@ class TasqClient:
82
84
  self.retry_server_errors = retry_server_errors
83
85
  self.session = requests.Session()
84
86
  self._configure_session()
85
- self.mp_context = multiprocessing.get_context("spawn")
86
87
 
87
88
  def push(self, contents: str, limit: int = 0) -> Optional[str]:
88
89
  """
@@ -240,9 +241,7 @@ class TasqClient:
240
241
  while True:
241
242
  task, timeout = self.pop()
242
243
  if task is not None:
243
- rt = RunningTask(
244
- self, id=task.id, contents=task.contents, mp_context=self.mp_context
245
- )
244
+ rt = RunningTask(self, id=task.id, contents=task.contents)
246
245
  try:
247
246
  yield rt
248
247
  rt.completed()
@@ -258,7 +257,7 @@ class TasqClient:
258
257
  def counts(self, rate_window: int = 0) -> QueueCounts:
259
258
  """Get the number of tasks in each state within the queue."""
260
259
  data = self._get(
261
- f"/counts?window={rate_window}",
260
+ f"/counts?window={rate_window}&includeModtime=1",
262
261
  {
263
262
  "pending": int,
264
263
  "running": int,
@@ -339,28 +338,28 @@ class RunningTask(Task):
339
338
  cancel() or completed() is called.
340
339
  """
341
340
 
342
- def __init__(
343
- self, client: TasqClient, *args, mp_context: Optional[BaseContext] = None, **kwargs
344
- ):
341
+ def __init__(self, client: TasqClient, *args, **kwargs):
345
342
  super().__init__(*args, **kwargs)
346
343
  self.client = client
347
- self._proc = (mp_context or multiprocessing).Process(
344
+ self._kill_queue = Queue()
345
+ self._thread = Thread(
348
346
  target=RunningTask._keepalive_worker,
349
347
  name="tasq-keepalive-worker",
350
348
  args=(
349
+ self._kill_queue,
351
350
  client,
352
351
  self.id,
353
352
  ),
354
353
  daemon=True,
355
354
  )
356
- self._proc.start()
355
+ self._thread.start()
357
356
 
358
357
  def cancel(self):
359
- if self._proc is None:
358
+ if self._thread is None:
360
359
  return
361
- self._proc.kill()
362
- self._proc.join()
363
- self._proc = None
360
+ self._kill_queue.put(None)
361
+ self._thread.join()
362
+ self._thread = None
364
363
 
365
364
  def completed(self):
366
365
  self.cancel()
@@ -368,6 +367,7 @@ class RunningTask(Task):
368
367
 
369
368
  @staticmethod
370
369
  def _keepalive_worker(
370
+ kill_queue: Queue,
371
371
  client: TasqClient,
372
372
  task_id: str,
373
373
  ):
@@ -375,8 +375,19 @@ class RunningTask(Task):
375
375
  try:
376
376
  client.keepalive(task_id)
377
377
  except Exception as exc: # pylint: disable=broad-except
378
+ # Ignore the error if we killed the thread during the
379
+ # keepalive call.
380
+ try:
381
+ kill_queue.get(block=False)
382
+ return
383
+ except Empty:
384
+ pass
378
385
  print(f"exception in tasq keepalive worker: {exc}", file=sys.stderr)
379
- time.sleep(client.keepalive_interval)
386
+ try:
387
+ kill_queue.get(timeout=client.keepalive_interval)
388
+ return
389
+ except Empty:
390
+ pass
380
391
 
381
392
 
382
393
  class TasqRemoteError(Exception):
@@ -1,3 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tasq-client-python
3
- Version: 0.1.13
3
+ Version: 0.1.15
4
+ Requires-Dist: requests