Solved

Best practices for multi-threading in Python

Posted on 2015-02-13
5
545 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
How to use pyenv 4 58
Python - desktop use 1 43
Awk To add just one column - modifying script from an answer 2 22
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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