如何在'joblib'Python库中共享变量

时间:2015-02-05 12:28:53

标签: python parallel-processing joblib

from joblib import Parallel, delayed

def func(v):
    temp.append(v)
    return

temp = []
Parallel(n_jobs=4)(delayed(func)(v) for v in range(10))
print temp

我想制作共享内存变量。但是temp的值是空的[]。我该怎么办?

对于其他方法,我尝试了pickle.dump并加载。但是存在锁定问题。请给我建议!

2 个答案:

答案 0 :(得分:3)

from joblib import Parallel, delayed

def func(v):
    return v

temp = Parallel(n_jobs=4)(delayed(func)(v) for v in range(10))
print temp

delayed收集列表中func返回的输出,并在完成后返回。

答案 1 :(得分:0)

您需要使用multiprocessing.Manager.list,例如:

<com.google.android.gms.maps.MapView
            android:id="@+id/map_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        map_view.onCreate(savedInstanceState)
        map_view.onResume()
        MapsInitializer.initialize(context)
        map_view.getMapAsync(this)
}

override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"))
        val cameraPosition = CameraPosition.Builder().target(sydney).zoom(12f).build()
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
    }

from joblib import Parallel, delayed from multiprocessing import Manager manager = Manager() temp = manager.list() def func(v, temp): temp.append(v) return _ = Parallel(n_jobs=4)(delayed(func)(v, temp) for v in range(10))

temp[:]