473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

using threads with for-loops

Hello,

I wrote a program that does essentially the following:

for rule in rules:
for line in line_list:
line = my_apply(rule, line)

line_list contains the lines of some input text.

To "apply a rule" always means to

1. do some regex matches on line
2. substitute something in line
My question is: Given this "architecture", does it make sense
to use threads? And if so, how?

Klaus
Jul 18 '05 #1
9 1410
Klaus Neuner wrote:
Hello,

I wrote a program that does essentially the following:

for rule in rules:
for line in line_list:
line = my_apply(rule, line)

line_list contains the lines of some input text.

To "apply a rule" always means to

1. do some regex matches on line
2. substitute something in line
My question is: Given this "architecture", does it make sense
to use threads? And if so, how?

Klaus


It depends on how many items are in rules and how long my_apply() takes.
Jul 18 '05 #2
Klaus Neuner wrote:
Hello,

I wrote a program that does essentially the following:

for rule in rules:
for line in line_list:
line = my_apply(rule, line)

line_list contains the lines of some input text.

To "apply a rule" always means to

1. do some regex matches on line
2. substitute something in line

My question is: Given this "architecture", does it make sense
to use threads? And if so, how?


The code is (based on what you give above) "CPU bound",
which means you will not see any advantage in using
threads. Threads don't magically make anything go
faster, and in fact have a certain overhead for the context
switch, so no, it makes no sense to use threads here.

-Peter
Jul 18 '05 #3
Peter Hansen wrote:
Klaus Neuner wrote:
Hello,

I wrote a program that does essentially the following:

for rule in rules:
for line in line_list:
line = my_apply(rule, line)

line_list contains the lines of some input text.

To "apply a rule" always means to

1. do some regex matches on line
2. substitute something in line

My question is: Given this "architecture", does it make sense
to use threads? And if so, how?


The code is (based on what you give above) "CPU bound",
which means you will not see any advantage in using
threads.


Have you ever seen machines with more than one CPU? The code above is
perfectly suited for parallelization.

Mathias
Jul 18 '05 #4
Mathias Waack wrote:
Peter Hansen wrote:
Klaus Neuner wrote:
I wrote a program that does essentially the following:

for rule in rules:
for line in line_list:
line = my_apply(rule, line)

line_list contains the lines of some input text.

To "apply a rule" always means to

1. do some regex matches on line
2. substitute something in line

My question is: Given this "architecture", does it make sense
to use threads? And if so, how?
The code is (based on what you give above) "CPU bound",
which means you will not see any advantage in using
threads.


Have you ever seen machines with more than one CPU?


Why yes, I have! And have *you* seen an implementation
of Python which will effectively use those multiple CPUs
in code like that above which runs in a single process?

And do you think it likely that the OP is dealing with
a multiple CPU situation, but managed to forget to
mention it? I didn't think it likely, which is why when
I considered the multi-CPU situation I discarded the
idea. Perhaps, however, I was too quick to judge...
The code above is perfectly suited for parallelization.


Yes, it is. Once you take into account implementation
issues (e.g. the GIL), would you still think so?

-Peter
Jul 18 '05 #5
Peter Hansen wrote:
Mathias Waack wrote:
Have you ever seen machines with more than one CPU?


Why yes, I have! And have *you* seen an implementation
of Python which will effectively use those multiple CPUs
in code like that above which runs in a single process?


Ok, I've lost: I haven't seen such implementation and don't know much
about the thread-layer of Python.
The code above is perfectly suited for parallelization.


Yes, it is. Once you take into account implementation
issues (e.g. the GIL), would you still think so?


Depends. Which means: don't know. If I would start thinking about
creating threads to gain a speedup, I would even think about
switching to another programming language.

Mathias
Jul 18 '05 #6
Mathias Waack wrote:
I haven't seen such implementation and don't know much
about the thread-layer of Python.
Unfortunately, there is something called the Global
Interpreter Lock (GIL), which means that even though
native threads are (generally) used for Python threads,
only one of those threads can be active in the interpreter
at any time, even if there are multiple CPUs present.
Depends. Which means: don't know. If I would start thinking about
creating threads to gain a speedup, I would even think about
switching to another programming language.


I believe some work has been done in this area to make
Python take advantage of multiple CPU systems, but
I believe your approach (switch languages) is still one of
the best options. Another is to arrange your application
to run as multiple processes, but this isn't quite as
simple as just using multiple threads.

-Peter
Jul 18 '05 #7

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:Kv********************@powergate.ca...
The code above is perfectly suited for parallelization.


Yes, it is. Once you take into account implementation
issues (e.g. the GIL), would you still think so?


Orrr .... you could cheat and use f.ex. PYRO to disguise a set of *Python
Applications* as Threads; The Applications could then be distributed however
which way you want ;-)
Jul 18 '05 #8
Peter Hansen wrote:
Mathias Waack wrote:
Depends. Which means: don't know. If I would start thinking about
creating threads to gain a speedup, I would even think about
switching to another programming language.


I believe some work has been done in this area to make
Python take advantage of multiple CPU systems, but
I believe your approach (switch languages) is still one of
the best options. Another is to arrange your application
to run as multiple processes, but this isn't quite as
simple as just using multiple threads.


The java people have done a lot to speed up java threads. Without any
real success (just my opinion) - java programs are just slow. There
are classes of problems which can be easily solved using python, and
there are problems not very well suited for pythonic solutions.
Thats a fact and nobody should waste her time to force python into
the wrong direction.
And I think its fair to let other languages live. We should be fair
winners;)

Mathias
Jul 18 '05 #9
Mathias Waack <M.*****@gmx.de> wrote:
...
Have you ever seen machines with more than one CPU?


Why yes, I have! And have *you* seen an implementation
of Python which will effectively use those multiple CPUs
in code like that above which runs in a single process?


Ok, I've lost: I haven't seen such implementation and don't know much
about the thread-layer of Python.


There are, as far as I know, three complete implementations of Python
(plus several add-on bits and pieces and unfinished ones): CPython,
Jython, and IronPython. CPython uses its own dedicated virtual machine,
and its threads are subject to a global per-interpreter lock.

However, in lieu of dedicated virtual machines, Jython relies on the
JVM, and IronPython relies on Microsoft CLR's, and I believe both of
those VMs have no global interpreter lock. I have no multi-CPU machine
at hand that can run Microsoft's CLR, but I do have a Powermac with two
CPUs, MacOSX 10.3.5, and a JVM (1.4.2 is the latest one, I believe).
So, if you can suggest a test to show whether Jython there can in fact
effectively use both CPU's, I'll be glad to run it and let everybody
know (I'm a bit rusty on recent Java VMs, so I don't know if I need any
special incantations to tell them to run on many CPUs, or what). I'm
not sure IronPython runs fully on Mono, and neither am I sure the
current release of Mono on MacOSX is able to use multiple CPUs for
threading, but if somebody can find out and suggest a definitive test on
the matter, again I'll be glad to run it and report to the list.

Net of such niggling issues, one might say that _most_ (hey, 2 out of 3,
right?-) current complete implementations of Python can do "free
threading" with no global per-interpreter lock, and thus in theory
should be able to use multiple CPUs productively in multiple CPU-bound
threads of a sinble process -- assuming, say, Java or C# can do so, I
see no reason, in principle, why Python shouldn't be able to, when it
runs on the same underlying VM as Java or C# respectively.

The code above is perfectly suited for parallelization.


Yes, it is. Once you take into account implementation
issues (e.g. the GIL), would you still think so?


Depends. Which means: don't know. If I would start thinking about
creating threads to gain a speedup, I would even think about
switching to another programming language.


....or another implementation of Python, if you're currently using
CPython and some limitation in it is a big problem for you...
Alex
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The...
6
by: Evan David Light | last post by:
After agonizing over this problem for a few days, I've decided to seek help. No, not the variety that involes a jacket that zips up the back but this august body of intrepid individuals. I've...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: Lucas Tam | last post by:
Is there a document online which details the advantages of using a Threadpool? My application uses a user configuration amount of threads which does the following: Main Thread Gives Work to...
4
by: Mrinal Kamboj | last post by:
Hi , I had a doubt regarding a piece of code with me , that has to do with System.Threading.Thread class . In it user instantiates an array of Thread class and to all of them assign a method...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
6
by: Ricardo Vazquez | last post by:
Hello everybody, I'm programming a TCP server. During the stress tests I noticed that maybe my socket-receiving thread became deaf after an hour of heavy stress. I think that the reason could...
19
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources...
3
by: Steven Blair | last post by:
I have been watching an MSDN video on the PFX Task class and have a question. Here is my scenario: TcpListener waits on incoming connections. Once a new connection is established, a new Task...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.