Solved

Best practices for multi-threading in Python

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 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