l3uddz / plex_autoscan

Script to assist sonarr/radarr with plex imports. Will only scan the folder that has been imported, instead of the whole library section.
GNU General Public License v3.0
396 stars 70 forks source link

Adding support for Python3 without breaking Python2 compatibility. #83

Closed sabrsorensen closed 5 years ago

sabrsorensen commented 5 years ago

Everything else seems Python3-ready, this was the only hiccup I've found so far.

sabrsorensen commented 5 years ago

Realized I hadn't yet gone through the new GDrive auth workflow, ran into an execution error on using raw_input when calling scan.py authorize. Added a commit to handle Python3's renaming of raw_input to input and updated the shebang to find Python instead of Python2.7.

l3uddz commented 5 years ago

Thanks for the PR.

I would love to accept this, however we need to figure out a solution for the PriorityLock issue.

If you create a new test project and place the threads.py into a new utils package.

Install python2 and python3 in your venv so you can alternate between the two.

This showcases the issue:

#!/usr/bin/env python
import time

from utils.threads import Thread, PriorityLock

############################################################
# MISC
############################################################

def test_thread(lock, priority):
    lock.acquire(priority)
    try:
        print("Hello from priority: %s" % str(priority))
        time.sleep(5)
    finally:
        lock.release()
    print("Finished priority: %s" % str(priority))

############################################################
# MAIN
############################################################

if __name__ == "__main__":
    lock = PriorityLock()
    threads = Thread()
    for pos in reversed(range(0, 100)):
        threads.start(test_thread, args=[lock, pos], track=True)
    print("Started first batch of threads")
    time.sleep(15)
    for pos in reversed(range(0, 100)):
        threads.start(test_thread, args=[lock, pos], track=True)
    print("Started second batch of threads")
    threads.join()
    print("All threads finished")

With python2 we get:

Hello from priority: 99
Started first batch of threads
Finished priority: 99
Hello from priority: 0
Finished priority: 0
 Hello from priority: 1
Finished priority: 1
Hello from priority: 2
Started second batch of threads
Finished priority: 2
 Hello from priority: 0
Finished priority: 0
 Hello from priority: 1

With python3:

Hello from priority: 99
Started first batch of threads
Finished priority: 99
Hello from priority: 0
Finished priority: 0
Hello from priority: 1
Finished priority: 1
Hello from priority: 2
Started second batch of threads
Exception in thread Thread-128:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "app.py", line 12, in test_thread
    lock.acquire(priority)
  File "/Users/l3uddz/Projects/random_tests/utils/threads.py", line 25, in acquire
    self._waiter_queue.put((priority, event))
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/queue.py", line 149, in put
    self._put(item)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/queue.py", line 233, in _put
    heappush(self.queue, item)
TypeError: '<' not supported between instances of 'Event' and 'Event'

If we are able to get the priority lock class working with python 3 then we are in business

sabrsorensen commented 5 years ago

I added the timestamp at the time of Event insertion in the PriorityQueue, which adds another layer of sorting that shouldn't have any duplicate entries, hopefully eliminating the need to compare the Event objects.

Also added your test script to the repo, and verified these changes function correctly in Python2 and Python3.

l3uddz commented 5 years ago

Good job!

Thanks for your contribution, python3 support was long overdue

sabrsorensen commented 5 years ago

Thank you for your help getting an acceptable Python3 solution put together, and of course for the project as a whole!