Python Forum
5 threading methods, none sped up the program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
5 threading methods, none sped up the program
#5
(Sep-23-2018, 04:23 AM)Gribouillis Wrote: Did you profile the code to find the bottlenecks?

I doubt there would be bottlenecks because all the code does is open a text file and then loop through each line in that text file.

(Sep-24-2018, 07:21 AM)nilamo Wrote: Also, I don't understand your use_coroutines() function, since it... doesn't use coroutines lol.
I just adapted the following code from Beazley's Python Cookbook for my own purposes:

# A very simple example of a coroutine/generator scheduler

# Two simple generator functions
def countdown(n):
    while n > 0:
        print("T-minus", n)
        yield
        n -= 1
    print("Blastoff!")

def countup(n):
    x = 0
    while x < n:
        print("Counting up", x)
        yield
        x += 1

from collections import deque

class TaskScheduler:
    def __init__(self):
        self._task_queue = deque()

    def new_task(self, task):
        '''
        Admit a newly started task to the scheduler
        '''
        self._task_queue.append(task)

    def run(self):
        '''
        Run until there are no more tasks
        '''
        while self._task_queue:
            task = self._task_queue.popleft()
            try:
                # Run until the next yield statement
                next(task)
                self._task_queue.append(task)
            except StopIteration:
                # Generator is no longer executing
                pass

# Example use
sched = TaskScheduler()
sched.new_task(countdown(10))
sched.new_task(countdown(5))
sched.new_task(countup(15))
sched.run()

(Sep-24-2018, 01:30 AM)woooee Wrote: It appears that you are reading some of the same files in different threads. You start in different places, but starting with 1, will still read 2, which possibly will conflict with the process starting with 2. Read the directory and split the files up into 5 pieces and try passing each one of the 5 pieces to a different Process to see if this is the problem.

Could you show a simplified example because I don't understand exactly what you're referring to?
Reply


Messages In This Thread
RE: 5 threading methods, none sped up the program - by bobsmith76 - Sep-25-2018, 01:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,865 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,130 Oct-01-2021, 08:32 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020