Distributask Class
distributask.Distributask
The Distributask class contains the core features of distributask, including creating and executing the task queue, managing workers using the Vast.ai API, and uploading files and directories using the Hugging Face API.
Source code in distributask/distributask.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
app: Celery = Celery('distributask', broker=redis_url, backend=redis_url)
class-attribute
instance-attribute
pool: ConnectionPool = None
class-attribute
instance-attribute
redis_client: Redis = self.get_redis_connection()
class-attribute
instance-attribute
registered_functions: dict = {}
class-attribute
instance-attribute
settings = {'HF_REPO_ID': hf_repo_id, 'HF_TOKEN': hf_token, 'VAST_API_KEY': vast_api_key, 'REDIS_HOST': redis_host, 'REDIS_PASSWORD': redis_password, 'REDIS_PORT': redis_port, 'REDIS_USER': redis_username, 'BROKER_POOL_LIMIT': broker_pool_limit}
instance-attribute
__del__()
Destructor to clean up resources.
Source code in distributask/distributask.py
124 125 126 127 128 129 130 131 |
|
__init__(hf_repo_id=os.getenv('HF_REPO_ID'), hf_token=os.getenv('HF_TOKEN'), vast_api_key=os.getenv('VAST_API_KEY'), redis_host=os.getenv('REDIS_HOST', 'localhost'), redis_password=os.getenv('REDIS_PASSWORD', ''), redis_port=os.getenv('REDIS_PORT', 6379), redis_username=os.getenv('REDIS_USER', 'default'), broker_pool_limit=os.getenv('BROKER_POOL_LIMIT', 1))
Initialize the Distributask object with the provided configuration parameters. Also sets some default settings in Celery and handles cleanup of Celery queue and Redis server on exit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hf_repo_id |
str
|
Hugging Face repository ID. |
getenv('HF_REPO_ID')
|
hf_token |
str
|
Hugging Face API token. |
getenv('HF_TOKEN')
|
vast_api_key |
str
|
Vast.ai API key. |
getenv('VAST_API_KEY')
|
redis_host |
str
|
Redis host. Defaults to "localhost". |
getenv('REDIS_HOST', 'localhost')
|
redis_password |
str
|
Redis password. Defaults to an empty string. |
getenv('REDIS_PASSWORD', '')
|
redis_port |
int
|
Redis port. Defaults to 6379. |
getenv('REDIS_PORT', 6379)
|
redis_username |
str
|
Redis username. Defaults to "default". |
getenv('REDIS_USER', 'default')
|
broker_pool_limit |
int
|
Celery broker pool limit. Defaults to 1. |
getenv('BROKER_POOL_LIMIT', 1)
|
Raises:
Type | Description |
---|---|
ValueError
|
If any of the required parameters (hf_repo_id, hf_token, vast_api_key) are not provided. |
Source code in distributask/distributask.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
call_function_task(func_name, args_json)
Creates Celery task that executes a registered function with provided JSON arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_name |
str
|
The name of the registered function to execute. |
required |
args_json |
str
|
JSON string representation of the arguments for the function. |
required |
Returns:
Name | Type | Description |
---|---|---|
any |
any
|
Celery.app.task object, represents result of the registered function |
Raises:
Type | Description |
---|---|
ValueError
|
If the function name is not registered. |
Exception
|
If an error occurs during the execution of the function. The task will retry in this case. |
Source code in distributask/distributask.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
create_instance(offer_id, image, module_name, env_settings, command)
Create an instance on the Vast.ai platform. Passes in some useful Celery settings by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offer_id |
str
|
The ID of the offer to create the instance from. |
required |
image |
str
|
The image to use for the instance. (example: RaccoonResearch/distributask-test-worker) |
required |
module_name |
str
|
The name of the module to run on the instance, configured to be a docker file (example: distributask.example.worker) |
required |
command |
str
|
Command that initializes celery worker. Has default command with specific settings if not passed in. These settings have |
required |
env_settings |
Dict
|
Used to pass in environment variables to the Vast.ai instance. This is a dictionary with keys of the |
required |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary representing the created instance. |
Raises:
Type | Description |
---|---|
ValueError
|
If the Vast.ai API key is not set in the environment. |
Exception
|
If there is an error while creating the instance. |
Source code in distributask/distributask.py
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
|
delete_file(repo_id, path_in_repo)
Delete a file from a Hugging Face repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The ID of the repository. |
required |
path_in_repo |
str
|
The path of the file to delete within the repository. |
required |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs during the deletion process. |
Source code in distributask/distributask.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
destroy_instance(instance_id)
Destroy an instance on the Vast.ai platform.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_id |
str
|
The ID of the instance to destroy. |
required |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary representing the result of the destroy operation. |
Source code in distributask/distributask.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
|
execute_function(func_name, args)
Execute a registered function as a Celery task with provided arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_name |
str
|
The name of the function to execute. |
required |
args |
dict
|
Arguments to pass to the function. |
required |
Returns:
Type | Description |
---|---|
AsyncResult
|
celery.result.AsyncResult: An object representing the asynchronous result of the task. |
Source code in distributask/distributask.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
file_exists(repo_id, path_in_repo)
Check if a file exists in a Hugging Face repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The ID of the repository. |
required |
path_in_repo |
str
|
The path of the file to check within the repository. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the file exists in the repository, False otherwise. |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs while checking the existence of the file. |
Source code in distributask/distributask.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
get_env(key, default=None)
Retrieve a value from the configuration or .env file, with an optional default if the key is not found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
The key to look for in the settings. |
required |
default |
any
|
The default value to return if the key is not found. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
any |
any
|
The value from the settings if the key exists, otherwise the default value. |
Source code in distributask/distributask.py
194 195 196 197 198 199 200 201 202 203 204 205 |
|
get_node_log(node, wait_time=2)
Get the log of the Vast.ai instance that is passed in. Makes an api call to tell the instance to send the log, and another one to actually retrive the log Args: node (Dict): the node that corresponds to the Vast.ai instance you want the log from wait_time (int): how long to wait in between the two api calls described above
Returns:
Name | Type | Description |
---|---|---|
str |
the log of the instance requested. If anything else other than a code 200 is received, return None |
Source code in distributask/distributask.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
get_redis_connection(force_new=False)
Returns Redis connection. If it already exists, returns current connection. If it does not exist, its create a new Redis connection using a connection pool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force_new |
bool
|
Force the creation of a new connection if set to True. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Redis |
Redis
|
A Redis connection object. |
Source code in distributask/distributask.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
get_redis_url()
Construct a Redis URL from the configuration settings.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A Redis URL string. |
Raises:
Type | Description |
---|---|
ValueError
|
If any required Redis connection parameter is missing. |
Source code in distributask/distributask.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
get_settings()
Return settings of distributask instance.
Source code in distributask/distributask.py
144 145 146 147 148 |
|
initialize_dataset(**kwargs)
Initialize a Hugging Face repository if it doesn't exist. Reads Hugging Face info from config or .env
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
kwargs that can be passed into the HfApi.create_repo function. |
{}
|
Raises:
Type | Description |
---|---|
HTTPError
|
If repo cannot be created due to connection error other than repo not existing |
Source code in distributask/distributask.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
list_files(repo_id)
Get a list of files from a Hugging Face repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The ID of the repository. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
A list of file paths in the repository. |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs while retrieving the list of files. |
Source code in distributask/distributask.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
log(message, level='info')
Log a message with the specified level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
The message to log. |
required |
level |
str
|
The logging level. Defaults to "info". |
'info'
|
Source code in distributask/distributask.py
133 134 135 136 137 138 139 140 141 142 |
|
monitor_tasks(tasks, update_interval=1, show_time_left=True, print_statements=True)
Monitor the status of the tasks on the Vast.ai nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tasks |
List
|
A list of the tasks to monitor. Should be a list of the results of execute_function. |
required |
update_interval |
bool
|
Number of seconds the status of tasks are updated. |
1
|
show_time_left |
bool
|
Show the estimated time left to complete tasks using the tqdm progress bar |
True
|
print_statments |
bool
|
Allow printing of status of task queue |
required |
Raises:
Type | Description |
---|---|
Exception
|
If error in the process of executing the tasks |
Source code in distributask/distributask.py
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
register_function(func)
Decorator to register a function so that it can be invoked as a Celery task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
callable
|
The function to register. |
required |
Returns:
Name | Type | Description |
---|---|---|
callable |
callable
|
The original function, now registered as a callable task. |
Source code in distributask/distributask.py
237 238 239 240 241 242 243 244 245 246 247 248 |
|
rent_nodes(max_price, max_nodes, image, module_name, env_settings=None, command=None)
Rent nodes as an instance on the Vast.ai platform.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_price |
float
|
The maximum price per hour for the nodes. |
required |
max_nodes |
int
|
The maximum number of nodes to rent. |
required |
image |
str
|
The image to use for the nodes. |
required |
module_name |
str
|
The name of the module to run on the nodes. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
List[Dict]: A list of dictionaries representing the rented nodes. If error is encountered |
List[Dict]
|
trying to rent, it will retry every 5 seconds. |
Source code in distributask/distributask.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
|
search_offers(max_price)
Search for available offers to rent a node as an instance on the Vast.ai platform.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_price |
float
|
The maximum price per hour for the instance. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
List[Dict]: A list of dictionaries representing the available offers. |
Raises:
Type | Description |
---|---|
RequestException
|
If there is an error while making the API request. |
Source code in distributask/distributask.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
|
terminate_nodes(nodes)
Terminate the instances of rented nodes on Vast.ai.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
List[Dict]
|
A list of dictionaries representing the rented nodes. |
required |
Raises:
Type | Description |
---|---|
Exception
|
If error in destroying instances. |
Source code in distributask/distributask.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
|
update_function_status(task_id, status)
Update the status of a function task as a new Redis key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_id |
str
|
The ID of the task. |
required |
status |
str
|
The new status to set. |
required |
Source code in distributask/distributask.py
265 266 267 268 269 270 271 272 273 274 |
|
upload_directory(dir_path)
Upload a directory to a Hugging Face repository. Can be used to reduce frequency of Hugging Face API calls if you are rate limited while using the upload_file function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path |
str
|
The path of the directory to upload. |
required |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs during the upload process. |
Source code in distributask/distributask.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
upload_file(file_path)
Upload a file to a Hugging Face repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
The path of the file to upload. |
required |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs during the upload process. |
Source code in distributask/distributask.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|