Solved

Best practices for multi-threading in Python

Posted on 2015-02-13
5
518 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

776 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