Solved

Best practices for multi-threading in Python

Posted on 2015-02-13
5
485 Views
Last Modified: 2015-02-18
Hello,

I'm fairly new to Python; and I'm trying to find some resources that properly explains how to structure a multi-threaded Python project. This will be running in GNU/Linux.

My main concern is that I have a dictionary that needs to be read by all the threads; would it be possible to access the dictionary in each thread without explicitly passing the variable into each thread?

Also, I would need to write into multiple files; would it be better to have one thread that accesses these files, or is Python/Unix smart enough to allow one thread to finish writing the entire line before another thread jumps in to write something?

Appreciate any advice on this!
0
Comment
Question by:Errang Genevre
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40610076
@Errang Genevre

Can use use a global variable for the dictionary?
0
 

Author Comment

by:Errang Genevre
ID: 40610239
And import it into the multi threaded process?
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 40610245
Have you tried using the Global keyword to specify a variable inside a function?
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
ID: 40610294
Remember that Python is not thread-safe unless you put effort into baking it into your application.  Any variables you declare, by default, is part of the global space.  It only becomes a thread-local variable if you declare it in a local data space.  

Keep in mind that also means a global variable can be *changed* from multiple threads...
0
 

Author Closing Comment

by:Errang Genevre
ID: 40616097
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now