Solved

Best practices for multi-threading in Python

Posted on 2015-02-13
5
606 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 51

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

707 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