Skip to content

Commit

Permalink
Merge pull request #26 from AlexVerrico/master
Browse files Browse the repository at this point in the history
Add Python 3 Support
  • Loading branch information
Salandora authored Apr 8, 2020
2 parents a2964c8 + aae2b41 commit d572aa4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
43 changes: 32 additions & 11 deletions octoprint_filemanager/ThreadPool.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
# http://code.activestate.com/recipes/577187-python-thread-pool/
# based on: https://www.metachris.com/2016/04/python-threadpool/
# which was based on http://code.activestate.com/recipes/577187-python-thread-pool/

import sys
IS_PY2 = sys.version_info < (3, 0)

if IS_PY2:
from Queue import Queue
else:
from queue import Queue

from Queue import Queue
from threading import Thread


class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
""" Thread executing tasks from a given tasks queue """
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()

def run(self):
while True:
func, args, kargs = self.tasks.get()
try: func(*args, **kargs)
except Exception, e: print e
self.tasks.task_done()
try:
func(*args, **kargs)
except Exception as e:
# An exception happened in this thread
print(e)
finally:
# Mark this task as done, whether an exception happened or not
self.tasks.task_done()


class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
""" Pool of threads consuming tasks from a queue """
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads): Worker(self.tasks)
for _ in range(num_threads):
Worker(self.tasks)

def add_task(self, func, *args, **kargs):
"""Add a task to the queue"""
""" Add a task to the queue """
self.tasks.put((func, args, kargs))

def map(self, func, args_list):
""" Add a list of tasks to the queue """
for args in args_list:
self.add_task(func, args)

def wait_completion(self):
"""Wait for completion of all the tasks in the queue"""
""" Wait for completion of all the tasks in the queue """
self.tasks.join()
3 changes: 2 additions & 1 deletion octoprint_filemanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def get_update_information(self):


__plugin_name__ = "FileManager"
__plugin_pythoncompat__ = ">=2.7,<4"


def __plugin_load__():
Expand All @@ -403,4 +404,4 @@ def __plugin_load__():
global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-FileManager"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.1.3"
plugin_version = "0.1.4"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit d572aa4

Please sign in to comment.