User
User

Reputation: 24731

Can global variables be accessed from a thread within a new process?

In the script's main thread, I setup a variable called queue and fill it with URLs. Then I create 8 processes using multiprocessing.Process and then those processes spawn 10 threads each using the threading library.

Within the thread worker (which was spawned by another process as per above), I have global queue.

Then will queue.get() act as expected? I've tried it and it seems to be okay during some tests and others not.

Question is, can global variables be accessed from another process and thread?

Upvotes: 2

Views: 2075

Answers (1)

Aaron
Aaron

Reputation: 2053

It is hard to understand what you are exactly asking. But there are two main questions here:

Can global variables be accessed from another process?

No, not without some form of inter-process communication, and even then you would be passing a copy of that variable to the other process. Each process has its own global state.

Can global variables be accessed from another thread?

Yes, a thread who lives in the same process can access a global variable, but you must ensure the safety of any memory that is accessed by multiple threads. Meaning, threads should not access writable memory at the same time as other threads or you run the risk of one thread writing to the memory while another thread attempts to read it.

Answering Question Above

If I understand the setup correctly, each of your child processes has their own global variable queue. Each of those queues should be accessible only to the threads that are spawned within that process.

Upvotes: 2

Related Questions